Example #1
0
        /// <summary>
        /// Add data
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="cacheCommand">Data cache command</param>
        /// <param name="databaseCommand">Database command</param>
        public void AddData <T>(AddDataCacheCommand <T> cacheCommand, ICommand databaseCommand) where T : BaseEntity <T>, new()
        {
            if (cacheCommand == null || databaseCommand == null)
            {
                return;
            }
            var cacheOperationConfig = DataCacheManager.Configuration.GetDataCacheOperationConfiguration(DataOperationType.Add);

            if (cacheOperationConfig == null)
            {
                return;
            }
            //database command starting event
            if ((cacheOperationConfig.TriggerTime & DataCacheOperationTriggerTime.Before) != 0)
            {
                databaseCommand.ListenStarting(CommandStartingEventHandler <T>, new CommandStartingEventParameter()
                {
                    CommandBehavior = CommandBehavior.Add,
                    Command         = databaseCommand,
                    Data            = cacheCommand.Data
                }, !cacheOperationConfig.Synchronous);
            }

            //save data to cache after command execute
            if ((cacheOperationConfig.TriggerTime & DataCacheOperationTriggerTime.After) != 0)
            {
                databaseCommand.ListenCallback(CommandCallbackEventHandler <T>, new CommandCallbackEventParameter()
                {
                    CommandBehavior = CommandBehavior.Add,
                    Command         = databaseCommand,
                    Data            = cacheCommand.Data
                });
            }
        }
Example #2
0
        /// <summary>
        /// Add data
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="cacheCommand">Data cache command</param>
        /// <returns>Return a ICommand instance</returns>
        public ICommand Add <T>(AddDataCacheCommand <T> cacheCommand) where T : BaseEntity <T>, new()
        {
            //get database add command
            ICommand databaseCommand = null;

            if (cacheCommand?.AddDataToDatabaseProxy != null)
            {
                databaseCommand = cacheCommand.AddDataToDatabaseProxy(cacheCommand.Data);
            }
            if (databaseCommand != null)
            {
                //trigger cache policy
                dataCachePolicyService.AddData(cacheCommand, databaseCommand);
            }
            return(databaseCommand);
        }