public void TestKeyGenerationGenericClass() { Assert.AreEqual(0, cache3.Hits); Assert.AreEqual(0, cache3.Misses); var methodInfo = typeof(GenericService <int>).GetMethod(nameof(GenericService <int> .MethodToCache1), new[] { typeof(double) }); var service = new GenericService <int>(); service.MethodToCache1(5.0); Assert.AreEqual(0, cache3.Hits); Assert.AreEqual(2, cache3.Misses); service.MethodToCache1(5.0); Assert.AreEqual(1, cache3.Hits); Assert.AreEqual(2, cache3.Misses); var key = CacheKey.GetKey(methodInfo, 5.0); cache3.Clear(key); service.MethodToCache1(5.0); Assert.AreEqual(1, cache3.Hits); Assert.AreEqual(4, cache3.Misses); service.MethodToCache1(5.0); Assert.AreEqual(2, cache3.Hits); Assert.AreEqual(4, cache3.Misses); }
public void TestKeyGeneration() { Assert.AreEqual(0, cache1.Hits); Assert.AreEqual(0, cache1.Misses); var methodInfo = typeof(Service1).GetMethod(nameof(Service1.MethodToCache1), new[] { typeof(double) }); var service = new Service1(); service.MethodToCache1(5.0); Assert.AreEqual(0, cache1.Hits); Assert.AreEqual(2, cache1.Misses); service.MethodToCache1(5.0); Assert.AreEqual(1, cache1.Hits); Assert.AreEqual(2, cache1.Misses); var key = CacheKey.GetKey(methodInfo, 5.0); cache1.Clear(key); service.MethodToCache1(5.0); Assert.AreEqual(1, cache1.Hits); Assert.AreEqual(4, cache1.Misses); service.MethodToCache1(5.0); Assert.AreEqual(2, cache1.Hits); Assert.AreEqual(4, cache1.Misses); }
public void TestKeyGenerationGenericMethod() { Assert.AreEqual(0, cache1.Hits); Assert.AreEqual(0, cache1.Misses); var service = new Service1(); service.MethodToCache2(5.0); Assert.AreEqual(0, cache1.Hits); Assert.AreEqual(2, cache1.Misses); service.MethodToCache2(5.0); Assert.AreEqual(1, cache1.Hits); Assert.AreEqual(2, cache1.Misses); var key = CacheKey.GetKey(() => service.MethodToCache2 <double>(5.0)); cache1.Clear(key); service.MethodToCache2(5.0); Assert.AreEqual(1, cache1.Hits); Assert.AreEqual(4, cache1.Misses); service.MethodToCache2(5.0); Assert.AreEqual(2, cache1.Hits); Assert.AreEqual(4, cache1.Misses); }
public ProjectDto GetProject(int id) { var cacheKey = CacheKey.GetKey(CacheKey.Projects, userSessionHelper.TeamId); var projects = this.cache.Get(cacheKey, () => this.projectRepository.GetProjects(this.userSessionHelper.TeamId), 60 * 60); return(projects.FirstOrDefault(s => s.Id == id)); }
public void Save(CreateProjectVM model) { model.CreatedById = userSessionHelper.UserId; model.TeamId = userSessionHelper.TeamId; this.projectRepository.Save(model); // Clear the cache so that new data will be read and stored to cache when needed cache.Clear(CacheKey.GetKey(CacheKey.Projects, userSessionHelper.TeamId)); }
public void TestKeyGenerationLambdaFromConst() { const double doubleVal = 5.0; var service = new Service1(); Action cachedAction = () => service.MethodToCache1(doubleVal); Func <string> cacheKeyGetter = () => CacheKey.GetKey((Service1 s) => s.MethodToCache1(doubleVal)); AssertCacheWithClear(cache1, cachedAction, cacheKeyGetter); }
public void TestKeyGenerationLambdaFromInnerProperty() { const double doubleVal = 5.0; var testObject = new TestObj { TestObjProperty = new TestObj { DoubleProperty = doubleVal } }; var service = new Service1(); Action cachedAction = () => service.MethodToCache1(testObject.TestObjProperty.DoubleProperty); Func<string> cacheKeyGetter = () => CacheKey.GetKey((Service1 s) => s.MethodToCache1(testObject.TestObjProperty.DoubleProperty)); AssertCacheWithClear(cache1, cachedAction, cacheKeyGetter); }
public void TestKeyGenerationLambdaFromStaticField() { const double doubleVal = 5.0; TestObj.StaticDoubleField = doubleVal; var service = new Service1(); Action cachedAction = () => service.MethodToCache1(TestObj.StaticDoubleField); Func<string> cacheKeyGetter = () => CacheKey.GetKey((Service1 s) => s.MethodToCache1(TestObj.StaticDoubleField)); AssertCacheWithClear(cache1, cachedAction, cacheKeyGetter); }
public void TestKeyGeneration_CacheList() { Assert.AreEqual(0, cache1.Hits); Assert.AreEqual(0, cache1.Misses); var service = new MultiService(); var results = service.GetItems(new[] { "k1", "k2", "k3" }); Assert.AreEqual(0, cache1.Hits); Assert.AreEqual(3, cache1.Misses); Assert.AreEqual(results.Count, 3); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k1"); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k2"); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k3"); results = service.GetItems(new[] { "k1", "k2", "k3" }); Assert.AreEqual(3, cache1.Hits); Assert.AreEqual(3, cache1.Misses); Assert.AreEqual(results.Count, 3); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k1"); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k2"); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k3"); // Note: must use List<string>, not string[] here in order to get correct key var key = CacheKey.GetKey(() => service.GetItems(new List <string> { "k2" })); cache1.Clear(key); results = service.GetItems(new[] { "k1" }); Assert.AreEqual(4, cache1.Hits); Assert.AreEqual(3, cache1.Misses); Assert.AreEqual(results.Count, 1); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k1"); results = service.GetItems(new[] { "k2" }); Assert.AreEqual(4, cache1.Hits); Assert.AreEqual(4, cache1.Misses); Assert.AreEqual(results.Count, 1); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k2"); results = service.GetItems(new[] { "k1", "k2", "k3" }); Assert.AreEqual(7, cache1.Hits); Assert.AreEqual(4, cache1.Misses); Assert.AreEqual(results.Count, 3); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k1"); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k2"); CollectionAssert.Contains(results.Select(x => x.Id).ToArray(), "k3"); }
public void TestKeyGeneration_CacheList() { Assert.AreEqual(0, cache1.Hits); Assert.AreEqual(0, cache1.Misses); var service = new MultiService(); var results = service.GetItems(new[] { "k1", "k2", "k3" }); Assert.AreEqual(0, cache1.Hits); Assert.AreEqual(3, cache1.Misses); LinqAssert.Count(results, 3); LinqAssert.Any(results, r => r.Id == "k1"); LinqAssert.Any(results, r => r.Id == "k2"); LinqAssert.Any(results, r => r.Id == "k3"); results = service.GetItems(new[] { "k1", "k2", "k3" }); Assert.AreEqual(3, cache1.Hits); Assert.AreEqual(3, cache1.Misses); LinqAssert.Count(results, 3); LinqAssert.Any(results, r => r.Id == "k1"); LinqAssert.Any(results, r => r.Id == "k2"); LinqAssert.Any(results, r => r.Id == "k3"); // Note: must use List<string>, not string[] here in order to get correct key var key = CacheKey.GetKey(() => service.GetItems(new List <string> { "k2" })); cache1.Clear(key); results = service.GetItems(new[] { "k1" }); Assert.AreEqual(4, cache1.Hits); Assert.AreEqual(3, cache1.Misses); LinqAssert.Count(results, 1); LinqAssert.Any(results, r => r.Id == "k1"); results = service.GetItems(new[] { "k2" }); Assert.AreEqual(4, cache1.Hits); Assert.AreEqual(4, cache1.Misses); LinqAssert.Count(results, 1); LinqAssert.Any(results, r => r.Id == "k2"); results = service.GetItems(new[] { "k1", "k2", "k3" }); Assert.AreEqual(7, cache1.Hits); Assert.AreEqual(4, cache1.Misses); LinqAssert.Count(results, 3); LinqAssert.Any(results, r => r.Id == "k1"); LinqAssert.Any(results, r => r.Id == "k2"); LinqAssert.Any(results, r => r.Id == "k3"); }
public IEnumerable <ActivityDto> GeActivityItems(int teamId, int count) { var cacheKey = CacheKey.GetKey(CacheKey.ActivityStream, teamId); return(this.Cache.Get(cacheKey, () => { var activities = _activityRepository.GetActivityItems(teamId, count); foreach (var activity in activities) { if (activity.ObjectType == "Issue") { activity.ObjectUrl = "Issues/" + activity.ObjectId; if (String.Equals(activity.Description, "CREATED", StringComparison.OrdinalIgnoreCase)) { activity.NewState = ""; } else if (String.Equals(activity.Description, "CHANGED STATUS", StringComparison.OrdinalIgnoreCase)) { activity.Description = "changed status of"; activity.NewState = "from " + activity.OldState + " to " + activity.NewState; } else if (String.Equals(activity.Description, "Changed category", StringComparison.OrdinalIgnoreCase)) { activity.Description = "changed category of"; activity.NewState = "from " + activity.OldState + " to " + activity.NewState; } else if (String.Equals(activity.Description, "DUE DATE UPDATED", StringComparison.OrdinalIgnoreCase)) { activity.Description = "updated due date of"; activity.NewState = "to " + activity.NewState; } } else if (activity.ObjectType == "Comment") { activity.ObjectUrl = "Issues/" + activity.ObjectId; if (activity.Description.Equals("Commented", StringComparison.OrdinalIgnoreCase)) { activity.Description = "Commented on "; } } } return activities; }, 15)); }
public IEnumerable <ProjectDto> GetProjects(int teamId) { var cacheKey = CacheKey.GetKey(CacheKey.Projects, userSessionHelper.TeamId); return(this.cache.Get(cacheKey, () => this.projectRepository.GetProjects(teamId), 60 * 60)); }