예제 #1
0
        public void BulkPutToRedis()
        {
            var redisCacheEngine = ObjectFactory.GetObject <ICacheEngine>();

            Stopwatch watch = Stopwatch.StartNew();

            IList <string> ids = new List <string>();

            for (int i = 0; i < 10000; i++)
            {
                var errorClass = new Issue
                {
                    ApplicationId = "ds",
                    UserId        = "ds/ds",
                    CreatedOnUtc  = DateTime.Now,
                    Name          = "Test",
                    Id            = Guid.NewGuid().ToString(),
                    ErrorCount    = 212,
                    Rules         = new List <IMatchRule>(),
                    Status        = IssueStatus.Acknowledged
                };

                ids.Add(errorClass.Id);

                var cacheKey = CacheKey.ForProfile(new CacheProfile(1, "test", redisCacheEngine))
                               .WithKey(errorClass.Id)
                               .Create();

                redisCacheEngine.Put(new CacheItemInfo
                {
                    Item = errorClass,
                    Key  = cacheKey
                });
            }

            Console.WriteLine("Put 10000 items to Redis in {0}ms".FormatWith(watch.ElapsedMilliseconds));

            watch = Stopwatch.StartNew();

            foreach (string id in ids)
            {
                Issue item;
                redisCacheEngine.Get(CacheKey.ForProfile(new CacheProfile(1, "test", redisCacheEngine))
                                     .WithKey(id)
                                     .Create(), out item);

                Assert.That(item != null);
            }

            Console.WriteLine("Retrieved 10000 items from Redis in {0}ms".FormatWith(watch.ElapsedMilliseconds));
        }
예제 #2
0
        public void SetAndGetGroups()
        {
            var redisCacheEngine   = ObjectFactory.GetObject <ICacheEngine>("redis");
            var cacheConfiguration = ObjectFactory.GetObject <ICacheConfiguration>();
            var groups             = new GetGroupsResponse
            {
                Groups = new Page <Group>(new List <Group>
                {
                    new Group
                    {
                        Id             = "groups/1",
                        Name           = "test 1",
                        OrganisationId = "organisations/1"
                    },
                    new Group
                    {
                        Id             = "groups/2",
                        Name           = "test 2",
                        OrganisationId = "organisations/1"
                    },
                    new Group
                    {
                        Id             = "groups/3",
                        Name           = "test 3",
                        OrganisationId = "organisations/1"
                    }
                },
                                          new PagingStatus(10, 1, 3))
            };
            var request = new GetGroupsRequest
            {
                OrganisationId = "organisations/1",
                Paging         = new PageRequestWithSort(1, 10)
            };
            var cacheKey =
                CacheKey.ForProfile(cacheConfiguration.GetCacheProfile(CacheProfiles.Groups)).WithKey(
                    ((ICacheable)request).CacheItemKey).Create();

            redisCacheEngine.Put(new CacheItemInfo
            {
                Item = groups,
                Key  = cacheKey
            });

            GetGroupsResponse fromCacheGroups;

            redisCacheEngine.Get(cacheKey, out fromCacheGroups);

            Assert.That(fromCacheGroups.Groups != null);
        }
예제 #3
0
        private void InvalidateItem(CacheInvalidationItem item)
        {
            var cacheProfile = _cacheConfiguration.GetCacheProfile(item.CacheProfile);

            if (cacheProfile == null)
            {
                throw new InvalidOperationException("CacheProfile with key {0} does not exist.".FormatWith(item.CacheProfile));
            }

            if (item.CacheItemKey.IsNullOrEmpty())
            {
                GetCacheEngine(cacheProfile).Clear(cacheProfile);
            }
            else if (item.IsKeyPrefix)
            {
                Trace("Prefix Invalidation...Cache:={0}, Engine:={1}, Profile:={2}, Prefix:={3}, Timeout:={4}",
                      cacheProfile.ProfileName,
                      cacheProfile.Engine,
                      item.CacheProfile,
                      item.CacheItemKey,
                      cacheProfile.Timeout);

                GetCacheEngine(cacheProfile).Clear(cacheProfile, item.CacheItemKey);
            }
            else
            {
                Trace("Invalidating direct from key..Cache:={0}, Engine:={1}, Profile:={2}, Key:={3}, Timeout:={4}",
                      cacheProfile.ProfileName,
                      cacheProfile.Engine,
                      item.CacheProfile,
                      item.CacheItemKey,
                      cacheProfile.Timeout);

                GetCacheEngine(cacheProfile).Remove(CacheKey.ForProfile(cacheProfile)
                                                    .WithKey(item.CacheItemKey)
                                                    .Create());
            }
        }
예제 #4
0
 protected virtual void Remove(ICacheable cacheableItem, CacheProfile cacheProfile, ICacheEngine cacheEngine)
 {
     cacheEngine.Remove(CacheKey.ForProfile(cacheProfile)
                        .WithKey(cacheableItem.CacheItemKey)
                        .Create());
 }
예제 #5
0
        public void Intercept(IInvocation invocation)
        {
            if (invocation.Arguments == null || invocation.Arguments.Length == 0 || invocation.Arguments[0] as ICacheable == null)
            {
                Trace("Caching is not supported by this method, the first argument passed to the method must implement ICacheable.");
                invocation.Proceed();
                return;
            }

            var cacheableRequest = (ICacheable)invocation.Arguments[0];

            if (cacheableRequest.IgnoreCache)
            {
                Trace("Caching has been overridden for this method {0}, returning without caching.", invocation.Method.Name);
                invocation.Proceed();
                return;
            }

            CacheProfile cacheProfile = _cacheConfiguration.GetCacheProfile(cacheableRequest.CacheProfile);

            if (cacheProfile == null)
            {
                throw new InvalidOperationException("Cache profile with the key {0} does not exist".FormatWith(cacheableRequest.CacheProfile));
            }

            CacheKey cacheKey = CacheKey.ForProfile(cacheProfile)
                                .WithKey(cacheableRequest.CacheItemKey)
                                .Create();

            bool   found;
            object response = null;

            try
            {
                found = cacheableRequest.GetFromCache(cacheProfile.Engine, cacheKey, out response);
            }
            catch (Exception ex)
            {
                Error(ex);
                found = false;
            }

            if (!found)
            {
                Trace("CACHE MISS...CacheId:={0}, Engine:={1}, Profile:={2}, Key:={3}, Timeout:={4}",
                      cacheProfile.CacheId,
                      cacheProfile.Engine.Name,
                      cacheableRequest.CacheProfile,
                      cacheableRequest.CacheItemKey,
                      cacheProfile.Timeout);

                invocation.Proceed();

                try
                {
                    cacheProfile.Engine.Put(new CacheItemInfo
                    {
                        Item = invocation.ReturnValue,
                        Key  = cacheKey
                    });
                }
                catch (Exception e)
                {
                    Error(e);
                }
            }
            else
            {
                invocation.ReturnValue = response;

                Trace("CACHE HIT...Type:={5}, CacheId:={0}, Engine:={1}, Profile:={2}, Key:={3}, Timeout:={4}",
                      cacheProfile.CacheId,
                      cacheProfile.Engine.Name,
                      cacheableRequest.CacheProfile,
                      cacheableRequest.CacheItemKey,
                      cacheProfile.Timeout,
                      response.GetType().Name);
            }
        }