public void SlidingExpiredItemIsRemoved() { var stringKey = CacheKey.Create <string>("hello"); var myObject1 = CacheProvider.GetOrCreate(stringKey, () => new CacheValueOf <TestCacheObject>(new TestCacheObject("hello-1"), new StaticCachePolicy(TimeSpan.FromSeconds(1)))); Assert.NotNull(myObject1); Assert.That(myObject1.AlreadyExisted, Is.False); Assert.That(myObject1.WasInserted, Is.True); Assert.NotNull(myObject1.Value); if (CacheIsAsync) { Thread.Sleep(500); } Assert.NotNull(CacheProvider.GetValue <TestCacheObject>(stringKey)); Assert.NotNull(CacheProvider.Get <TestCacheObject>(stringKey)); Thread.Sleep(TimeSpan.FromSeconds(1.2d)); Assert.Null(CacheProvider.Get <TestCacheObject>(stringKey)); }
public void CanRemoveItemFromCacheWithDelegate() { var myObject1 = new TestCacheObject("test-1"); var myObject2 = new TestCacheObject("bob"); var myObject3 = new TestCacheObject("frank"); CacheProvider.AddOrChangeValue(CacheKey.Create <string>("my-1"), myObject1); CacheProvider.AddOrChangeValue(CacheKey.Create <StrongClassKey>(x => x.MyName = "bob"), myObject2); CacheProvider.AddOrChangeValue(CacheKey.Create <StrongClassKey>(x => x.MyName = "frank"), myObject3); if (CacheIsAsync) { Thread.Sleep(500); } var resultFilterClause = new ResultFilterClause(typeof(string), ResultFilterType.Any, 0); var scopeStartId = new HiveId(Guid.NewGuid()); var fromClause = new FromClause(scopeStartId.AsEnumerableOfOne(), HierarchyScope.AncestorsOrSelf, FixedStatusTypes.Published); var fieldPredicateExpression = new FieldPredicateExpression("title", ValuePredicateType.Equal, "blah"); //var key = new HiveQueryCacheKey(new QueryDescription(resultFilterClause, fromClause, fieldPredicateExpression, Enumerable.Empty<SortClause>())); var key = CacheKey.Create(new HiveQueryCacheKey(new QueryDescription(resultFilterClause, fromClause, fieldPredicateExpression, Enumerable.Empty <SortClause>()))); CacheProvider.AddOrChangeValue(key, myObject3); if (CacheIsAsync) { Thread.Sleep(500); } Assert.NotNull(CacheProvider.GetValue <TestCacheObject>(CacheKey.Create <string>("my-1"))); Assert.NotNull(CacheProvider.GetValue <TestCacheObject>(CacheKey.Create <StrongClassKey>(x => x.MyName = "bob"))); Assert.NotNull(CacheProvider.GetValue <TestCacheObject>(CacheKey.Create <string>("my-1"))); Assert.NotNull(CacheProvider.GetValue <TestCacheObject>(key)); CacheProvider.RemoveWhereKeyMatches <string>(x => x == "my-1"); CacheProvider.RemoveWhereKeyMatches <StrongClassKey>(x => x.MyName == "bob"); CacheProvider.RemoveWhereKeyMatches <HiveQueryCacheKey>(x => x.From.HierarchyScope == HierarchyScope.AncestorsOrSelf); // No check for async as removals should be instant Assert.Null(CacheProvider.Get <TestCacheObject>(CacheKey.Create <string>("my-1"))); Assert.Null(CacheProvider.GetValue <TestCacheObject>(CacheKey.Create <string>("my-1"))); Assert.Null(CacheProvider.Get <TestCacheObject>(CacheKey.Create <StrongClassKey>(x => x.MyName = "bob"))); Assert.NotNull(CacheProvider.Get <TestCacheObject>(CacheKey.Create <StrongClassKey>(x => x.MyName = "frank"))); }
public T GetValue <T>(string project, string name) { var cacheResult = CacheProvider.GetValue <T>(project + name); if (cacheResult.Result == CacheResultEnum.Found) { return(cacheResult.Item); } var item = DataProvider.GetValue <SettingsModel <T> >(x => x.Key == name, project); if (item == null) { throw new SettingsNotFoundException(name, project); } CacheProvider.AddOrUpdate(project + name, item.Value); return(item.Value); }
public void CanAddItemToCacheWithComplexKey() { var myObject1 = new TestCacheObject("test-1"); var myObject2 = new TestCacheObject("test-2"); var myObject3 = new TestCacheObject("test-3"); Assert.IsNull(CacheProvider.GetValue <TestCacheObject>(CacheKey.Create <string>("ah"))); Assert.IsNull(CacheProvider.GetValue <TestCacheObject>(CacheKey.Create <string>("ah"))); CacheProvider.AddOrChangeValue(CacheKey.Create("my-1"), myObject1); if (CacheIsAsync) { Thread.Sleep(500); } var retrieve1typed = CacheProvider.GetValue <TestCacheObject>(CacheKey.Create <string>("my-1")); Assert.NotNull(retrieve1typed); var retrieve1 = CacheProvider.Get <TestCacheObject>(CacheKey.Create("my-1")); Assert.That(retrieve1.Item, Is.EqualTo(myObject1)); CacheProvider.AddOrChangeValue(CacheKey.Create("my-2"), myObject2); CacheProvider.AddOrChangeValue(CacheKey.Create <StrongClassKey>(x => x.MyName = "bob"), myObject3); if (CacheIsAsync) { Thread.Sleep(500); } var retrieve2 = CacheProvider.GetValue <TestCacheObject>(CacheKey.Create("my-2")); var retrieve3 = CacheProvider.GetValue <TestCacheObject>(CacheKey.Create <StrongClassKey>(x => x.MyName = "bob")); Assert.That(retrieve1typed.Text, Is.EqualTo(myObject1.Text)); Assert.That(retrieve2, Is.EqualTo(myObject2)); Assert.That(retrieve3, Is.EqualTo(myObject3)); }