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 void HiveQueryCacheKeyToFromJson() { var guid = Guid.NewGuid(); var guid2 = Guid.NewGuid(); var guid3 = Guid.NewGuid(); var resultFilterClause = new ResultFilterClause(typeof(string), ResultFilterType.Any, 0); var hiveId = new HiveId(guid); var expected = new HiveId(guid2); var fullHiveId = new HiveId(new Uri("content://blah/"), "nhibernate", new HiveIdValue(guid3)); var scopeStartId = new HiveId(Guid.NewGuid()); var fromClause = new FromClause(scopeStartId.AsEnumerableOfOne(), HierarchyScope.AncestorsOrSelf, FixedStatusTypes.Published, "default-type", new[] { hiveId, expected, fullHiveId }); var fieldPredicateExpression = new FieldPredicateExpression("title", ValuePredicateType.Equal, "blah"); var fieldPredicateExpression2 = new FieldPredicateExpression("title", ValuePredicateType.Equal, "blah"); var binary = Expression.MakeBinary(ExpressionType.ExclusiveOr, fieldPredicateExpression, fieldPredicateExpression2); var aSortClause = new SortClause(new FieldSelectorExpression("tag"), SortDirection.Descending, 2); //var key = new HiveQueryCacheKey(new QueryDescription(resultFilterClause, fromClause, fieldPredicateExpression, Enumerable.Empty<SortClause>())); var key = CacheKey.Create(new HiveQueryCacheKey(new QueryDescription(resultFilterClause, fromClause, fieldPredicateExpression, aSortClause.AsEnumerableOfOne()))); var keyJson = key.ToJson(); var keyBack = (CacheKey <HiveQueryCacheKey>)keyJson; // var keyJsonTwice = keyBack.ToJson(); Assert.That(keyBack, Is.Not.Null); //Assert.That(keyJson, Is.EqualTo(keyJsonTwice)); //NOTE: Criteria does not deserialize at the moment, but caching implementation should find matching keys OK still if it maintains an inner collection of original key objects if (key.Original.Criteria != null) Assert.That(keyBack.Original.Criteria, Is.Not.Null); Assert.True(keyBack.Original.ResultFilters.Any()); Assert.That(keyBack.Original.ResultFilters.First().ResultType, Is.EqualTo(typeof(string))); Assert.That(keyBack.Original.SortClauses.Count(), Is.EqualTo(key.Original.SortClauses.Count())); Assert.That(keyBack.Original.SortClauses.FirstOrDefault().Direction, Is.EqualTo(SortDirection.Descending)); Assert.That(keyBack.Original.SortClauses.FirstOrDefault().Priority, Is.EqualTo(2)); Assert.That(keyBack.Original.From.HierarchyScope, Is.EqualTo(key.Original.From.HierarchyScope)); Assert.That(keyBack.Original.From.RequiredEntityIds.Count(), Is.EqualTo(3)); Assert.That(keyBack.Original.From.RequiredEntityIds.FirstOrDefault(), Is.EqualTo(hiveId)); Assert.That(keyBack.Original.From.RequiredEntityIds.FirstOrDefault().Value.Type, Is.EqualTo(HiveIdValueTypes.Guid)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(1), Is.EqualTo(expected)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(1).Value.Type, Is.EqualTo(HiveIdValueTypes.Guid)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(2), Is.EqualTo(fullHiveId)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(2).Value.Type, Is.EqualTo(HiveIdValueTypes.Guid)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(2).ProviderId, Is.EqualTo("nhibernate")); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(2).ProviderGroupRoot.ToString(), Is.EqualTo(fullHiveId.ProviderGroupRoot.ToString())); }
public void HiveQueryCacheKeyToFromJson() { var guid = Guid.NewGuid(); var guid2 = Guid.NewGuid(); var guid3 = Guid.NewGuid(); var resultFilterClause = new ResultFilterClause(typeof(string), ResultFilterType.Any, 0); var hiveId = new HiveId(guid); var expected = new HiveId(guid2); var fullHiveId = new HiveId(new Uri("content://blah/"), "nhibernate", new HiveIdValue(guid3)); var scopeStartId = new HiveId(Guid.NewGuid()); var fromClause = new FromClause(scopeStartId.AsEnumerableOfOne(), HierarchyScope.AncestorsOrSelf, FixedStatusTypes.Published, "default-type", new[] { hiveId, expected, fullHiveId }); var fieldPredicateExpression = new FieldPredicateExpression("title", ValuePredicateType.Equal, "blah"); var fieldPredicateExpression2 = new FieldPredicateExpression("title", ValuePredicateType.Equal, "blah"); var binary = Expression.MakeBinary(ExpressionType.ExclusiveOr, fieldPredicateExpression, fieldPredicateExpression2); var aSortClause = new SortClause(new FieldSelectorExpression("tag"), SortDirection.Descending, 2); //var key = new HiveQueryCacheKey(new QueryDescription(resultFilterClause, fromClause, fieldPredicateExpression, Enumerable.Empty<SortClause>())); var key = CacheKey.Create(new HiveQueryCacheKey(new QueryDescription(resultFilterClause, fromClause, fieldPredicateExpression, aSortClause.AsEnumerableOfOne()))); var keyJson = key.ToJson(); var keyBack = (CacheKey<HiveQueryCacheKey>)keyJson; // var keyJsonTwice = keyBack.ToJson(); Assert.That(keyBack, Is.Not.Null); //Assert.That(keyJson, Is.EqualTo(keyJsonTwice)); //NOTE: Criteria does not deserialize at the moment, but caching implementation should find matching keys OK still if it maintains an inner collection of original key objects if (key.Original.Criteria != null) Assert.That(keyBack.Original.Criteria, Is.Not.Null); Assert.True(keyBack.Original.ResultFilters.Any()); Assert.That(keyBack.Original.ResultFilters.First().ResultType, Is.EqualTo(typeof(string))); Assert.That(keyBack.Original.SortClauses.Count(), Is.EqualTo(key.Original.SortClauses.Count())); Assert.That(keyBack.Original.SortClauses.FirstOrDefault().Direction, Is.EqualTo(SortDirection.Descending)); Assert.That(keyBack.Original.SortClauses.FirstOrDefault().Priority, Is.EqualTo(2)); Assert.That(keyBack.Original.From.HierarchyScope, Is.EqualTo(key.Original.From.HierarchyScope)); Assert.That(keyBack.Original.From.RequiredEntityIds.Count(), Is.EqualTo(3)); Assert.That(keyBack.Original.From.RequiredEntityIds.FirstOrDefault(), Is.EqualTo(hiveId)); Assert.That(keyBack.Original.From.RequiredEntityIds.FirstOrDefault().Value.Type, Is.EqualTo(HiveIdValueTypes.Guid)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(1), Is.EqualTo(expected)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(1).Value.Type, Is.EqualTo(HiveIdValueTypes.Guid)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(2), Is.EqualTo(fullHiveId)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(2).Value.Type, Is.EqualTo(HiveIdValueTypes.Guid)); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(2).ProviderId, Is.EqualTo("nhibernate")); Assert.That(keyBack.Original.From.RequiredEntityIds.ElementAt(2).ProviderGroupRoot.ToString(), Is.EqualTo(fullHiveId.ProviderGroupRoot.ToString())); }
public static IQueryable <T> WithParentIds <T>(this IQueryable <T> source, HiveId parentId, params HiveId[] parentIds) where T : class { return(WithParentIds(source, FixedRelationTypes.DefaultRelationType, parentId.AsEnumerableOfOne().Concat(parentIds).ToArray())); }
public static IQueryable <T> ExcludeIds <T>(this IQueryable <T> source, HiveId excludeId, params HiveId[] excludeIds) where T : class { return(ExcludeIds(source, excludeId.AsEnumerableOfOne().Concat(excludeIds).ToArray())); }