public async Task WithAttachedQueryCache_RendersPlanToCache() { var keyManager = new DefaultQueryPlanCacheKeyManager(new GraphQLParser()); var cacheInstance = new MemoryCache(nameof(WithAttachedQueryCache_RendersPlanToCache)); var cache = new DefaultQueryPlanCacheProvider(cacheInstance); var builder = new TestServerBuilder() .AddGraphType <SimpleExecutionController>(); builder.AddSingleton <IGraphQueryPlanCacheProvider>(cache); builder.AddSingleton <IGraphQueryPlanCacheKeyManager>(keyManager); // configure an absolute expriation of a few seconds to ensure the plan remains in cache // long enough to be fetched by tis expected key builder.AddGraphQL(o => { o.CacheOptions.TimeToLiveInMilliseconds = 10000; }); var server = builder.Build(); var queryText = "query { simple{ simpleQueryMethod { property1} } }"; var expectedCacheKey = keyManager.CreateKey <GraphSchema>(queryText); var queryBuilder = server.CreateQueryContextBuilder(); queryBuilder.AddQueryText(queryText); var result = await server.ExecuteQuery(queryBuilder); Assert.IsNotNull(result); Assert.AreEqual(1, cache.Count); // ensure we can pull the plan directly from the cache instance var cachedPlan = cacheInstance.GetCacheItem(expectedCacheKey); Assert.IsNotNull(cachedPlan); // attempt eviction through the provider await cache.EvictAsync(expectedCacheKey); // ensure underlying cache instance was evicted cachedPlan = cacheInstance.GetCacheItem(expectedCacheKey); Assert.IsNull(cachedPlan); }
public async Task ForceEvict_RemovesFromCache() { var cache = new DefaultQueryPlanCacheProvider(); var text = "query Operation1{ simple { simpleQueryMethod { property1 __typename} } }"; var plan = this.CreatePlan(text); var hash = Guid.NewGuid().ToString("N"); var wasCached = await cache.TryCachePlanAsync(hash, plan); Assert.IsTrue(wasCached); var evicted = await cache.EvictAsync(hash); Assert.IsTrue(evicted); var found = await cache.TryGetPlanAsync(hash, out var foundPlan); Assert.IsFalse(found); Assert.IsNull(foundPlan); }