/// <summary> /// Creates the policy. /// </summary> /// <returns>The policy.</returns> /// <param name="item">Item.</param> protected CacheItemPolicy CreatePolicy(OpenSearchResponseCacheItem item, OpenSearchRequest request) { CacheItemPolicy policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.UtcNow.Add(item.OpenSearchResponse.Validity); log.DebugFormat("OpenSearch Cache [prepare to store] AbsoluteExpiration {1} {0}", item.OpenSearchUrl, policy.AbsoluteExpiration); policy.RemovedCallback = new CacheEntryRemovedCallback(this.EntryRemovedCallBack); if (item.OpenSearchResponse.Entity is IMonitoredOpenSearchable) { IMonitoredOpenSearchable mos = (IMonitoredOpenSearchable)item.OpenSearchResponse.Entity; var monitor = new OpenSearchableChangeMonitor(mos, request); log.DebugFormat("OpenSearch Cache [prepare to store] Monitor {1} {0} ", item.OpenSearchUrl, monitor.UniqueId); policy.ChangeMonitors.Add(monitor); } return policy; }
public void EntryRemovedCallBack(CacheEntryRemovedArguments arguments) { if ( arguments.CacheItem is OpenSearchResponseCacheItem ){ OpenSearchResponseCacheItem item = new OpenSearchResponseCacheItem(arguments.CacheItem); log.DebugFormat("OpenSearch Cache [remove] reason {1} {0}", item.OpenSearchUrl, arguments.RemovedReason); } }
/// <summary> /// Tries the replace with cache request. /// </summary> /// <param name="request">Request.</param> public void TryReplaceWithCacheRequest(ref OpenSearchRequest request) { Stopwatch watch = new Stopwatch(); watch.Start(); CacheItem it = cache.GetCacheItem(request.OpenSearchUrl.ToString()); if (it == null) return; OpenSearchResponseCacheItem item = new OpenSearchResponseCacheItem(it); watch.Stop(); log.DebugFormat("OpenSearch Cache [load] {0}", request.OpenSearchUrl); request = new CachedOpenSearchRequest(item.OpenSearchUrl, item.OpenSearchResponse, request.OriginalParameters, watch.Elapsed); }
/// <summary> /// Caches the response. /// </summary> /// <param name="request">Request.</param> /// <param name="response">Response.</param> public void CacheResponse(OpenSearchRequest request, ref IOpenSearchResponse response) { CacheItem it = cache.GetCacheItem(request.OpenSearchUrl.ToString()); if (it != null) return; if (response.Entity != null && !response.Entity.CanCache) return; var clonedResponse = response.CloneForCache(); if (clonedResponse == null) throw new InvalidOperationException(string.Format("Response cannot be cached because it is null. Check the CloneForCache of the response [{0}] or the CanCache() method of the Opensearchable [{1}] requested", response.GetType(), response.Entity.GetType())); OpenSearchResponseCacheItem item = new OpenSearchResponseCacheItem(request.OpenSearchUrl, clonedResponse); CacheItemPolicy policy = this.CreatePolicy(item, request); log.DebugFormat("OpenSearch Cache [store] {0}", request.OpenSearchUrl); cache.Set(item, policy); log.DebugFormat("OpenSearch Cache [count] {0}", cache.GetCount()); }