Пример #1
0
        public override void OnSuccess(MethodExecutionArgs args)
        {
            lock (syncLock)
            {
                if (connection == null)
                {
                    string configString = ConfigurationManager.AppSettings["CacheConnection"];
                    var    options      = ConfigurationOptions.Parse(configString);
                    connection = ConnectionMultiplexer.Connect(options);

                    _redisCachingConfiguration     = new RedisCachingBackendConfiguration();
                    CachingServices.DefaultBackend = RedisCachingBackend.Create(connection, _redisCachingConfiguration);
                }

                jsonSettings = new JsonSerializerSettings()
                {
                    TypeNameHandling           = TypeNameHandling.All,
                    PreserveReferencesHandling = PreserveReferencesHandling.All
                };

                _cacheDatabase = connection.GetDatabase();
            }

            var cacheKey = (string)args.MethodExecutionTag;

            if (_cacheDatabase.KeyExists(cacheKey))
            {
                _cacheDatabase.KeyExpireAsync(cacheKey, TimeSpan.FromMilliseconds(2), CommandFlags.None);
            }
        }
Пример #2
0
        private static void Main(string[] args)
        {
            AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
            LoggingServices.DefaultBackend = new ConsoleLoggingBackend();

            using (RedisServer.Start())
            {
                using (var connection = ConnectionMultiplexer.Connect("localhost:6380,abortConnect = False"))
                {
                    HandleConnectionEvents(connection);
                    RedisCachingBackendConfiguration configuration = GetConfiguration();

                    using (var backend = RedisCachingBackend.Create(connection, configuration))
                    {
                        using (RedisCacheDependencyGarbageCollector.Create(connection, configuration))
                        {
                            CachingServices.DefaultBackend = backend;
                            CachingServices.Profiles["StudentAccount"].AbsoluteExpiration =
                                TimeSpan.FromSeconds(10);
                            TestDirectInvalidation();
                            TestIndirectInvalidation();
                        }
                    }
                }
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Пример #3
0
        private static void Main(string[] args)
        {
            AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
            LoggingServices.DefaultBackend = new ConsoleLoggingBackend();

            // Uncomment the next line for detailed logging.
            // LoggingServices.DefaultBackend.DefaultVerbosity.SetMinimalLevel(LogLevel.Debug, LoggingRoles.Caching);

            using (RedisServer.Start())
            {
                using (var connection = ConnectionMultiplexer.Connect("localhost:6380,abortConnect = False"))
                {
                    connection.InternalError    += (sender, eventArgs) => Console.Error.WriteLine(eventArgs.Exception);
                    connection.ErrorMessage     += (sender, eventArgs) => Console.Error.WriteLine(eventArgs.Message);
                    connection.ConnectionFailed += (sender, eventArgs) => Console.Error.WriteLine(eventArgs.Exception);

                    var configuration = new RedisCachingBackendConfiguration
                    {
                        IsLocallyCached      = true,
                        SupportsDependencies = true
                    };

                    using (var backend = RedisCachingBackend.Create(connection, configuration))
                    {
                        // With Redis, we need at least one instance of the collection engine.
                        using (RedisCacheDependencyGarbageCollector.Create(connection, configuration))

                        {
                            CachingServices.DefaultBackend = backend;

                            // Configure the Account caching profile used in the AccountServices class.
                            CachingServices.Profiles["Account"].AbsoluteExpiration = TimeSpan.FromSeconds(10);

                            // Testing direct invalidation.
                            Console.WriteLine("Retrieving the customer for the 1st time should hit the database.");
                            CustomerServices.GetCustomer(1);
                            Console.WriteLine("Retrieving the customer for the 2nd time should NOT hit the database.");
                            CustomerServices.GetCustomer(1);
                            Console.WriteLine("This should invalidate the GetCustomer method.");
                            CustomerServices.UpdateCustomer(1, "New name");
                            Console.WriteLine("This should hit the database again because GetCustomer has been invalidated.");
                            CustomerServices.GetCustomer(1);

                            // Testing indirect invalidation (dependencies).
                            Console.WriteLine("Retrieving the account list for the 1st time should hit the database.");
                            AccountServices.GetAccountsOfCustomer(1);
                            Console.WriteLine("Retrieving the account list for the 2nt time should NOT hit the database.");
                            var accounts = AccountServices.GetAccountsOfCustomer(1);
                            Console.WriteLine("This should invalidate the accounts");
                            AccountServices.UpdateAccount(accounts.First());
                            Console.WriteLine(
                                "This should hit the database again because GetAccountsOfCustomer has been invalidated.");
                            AccountServices.GetAccountsOfCustomer(1);

                            Console.WriteLine("Done!");
                        }
                    }
                }
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            using (RedisServer.Start())
            {
                using (ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("localhost:6380,abortConnect = False"))
                {
                    RedisCachingBackendConfiguration configuration = new RedisCachingBackendConfiguration();

                    connection.ErrorMessage     += (sender, eventArgs) => Console.Error.WriteLine(eventArgs.Message);
                    connection.ConnectionFailed += (sender, eventArgs) => Console.Error.WriteLine(eventArgs.Exception);

                    using (var backend = new TwoLayerCachingBackendEnhancer(RedisCachingBackend.Create(connection, configuration)))
                        using (RedisCacheDependencyGarbageCollector.Create(connection, configuration)) // With Redis, we need at least one instance of the collection engine.
                        {
                            CachingServices.DefaultBackend = backend;
                            CachingServices.Profiles["Account"].AbsoluteExpiration = TimeSpan.FromSeconds(10);

                            // Testing direct invalidation.
                            Console.WriteLine("Retrieving the customer for the 1st time should hit the database.");
                            CustomerServices.GetCustomer(1);
                            Console.WriteLine("Retrieving the customer for the 2nd time should NOT hit the database.");
                            CustomerServices.GetCustomer(1);
                            Console.WriteLine("This should invalidate the GetCustomer method.");
                            CustomerServices.UpdateCustomer(1, "New name");
                            Console.WriteLine("This should hit the database again because GetCustomer has been invalidated.");
                            CustomerServices.GetCustomer(1);

                            // Testing indirect invalidation (dependencies).
                            Console.WriteLine("Retrieving the account list for the 1st time should hit the database.");
                            AccountServices.GetAccountsOfCustomer(1);
                            Console.WriteLine("Retrieving the account list for the 2nt time should NOT hit the database.");
                            var accounts = AccountServices.GetAccountsOfCustomer(1);
                            Console.WriteLine("This should invalidate the accounts");
                            AccountServices.UpdateAccount(accounts.First());
                            Console.WriteLine("This should hit the database again because GetAccountsOfCustomer has been invalidated.");
                            AccountServices.GetAccountsOfCustomer(1);

                            Console.WriteLine("Done!");
                        }
                }
            }
        }