public void AddDependency(string key, string dependencyKey) { var dependencies = _localCache.Get <IList <string> >(key, ReadStrategy.Immediate); if (dependencies == null) { dependencies = new List <string>(); _localCache.Insert(key, dependencies, CacheEvictionPolicy.Empty); } dependencies.Add(dependencyKey); }
public IDictionary <ContentReference, ContentType> ResolveContentTypes( IEnumerable <ContentReference> contentLinks) { var result = new Dictionary <ContentReference, ContentType>(); var catalogContentLinks = contentLinks.Where(l => _referenceConverter.GetContentType(l) == CatalogContentType.Catalog); foreach (var catalogContentLink in catalogContentLinks) { result.Add(catalogContentLink, _catalogContentType); } var notCachedLinks = new List <ContentReference>(); foreach (var contentLink in contentLinks) { var cached = _cache.Get(GetCacheKey(contentLink)) as ContentType; if (cached != null) { result.Add(contentLink, cached); } else { notCachedLinks.Add(contentLink); } } var nodeContentLinks = notCachedLinks.Where(l => _referenceConverter.GetContentType(l) == CatalogContentType.CatalogNode); var nodeIds = nodeContentLinks.Select(l => _referenceConverter.GetObjectId(l)); var entryLinks = notCachedLinks.Where(l => _referenceConverter.GetContentType(l) == CatalogContentType.CatalogEntry); var entryIds = entryLinks.Select(l => _referenceConverter.GetObjectId(l)); var ds = LoadMetaClassNames(entryIds, nodeIds); foreach (var keyPair in ResolveContentTypes(ds.Tables[0], CatalogContentType.CatalogEntry)) { result.Add(keyPair.Key, keyPair.Value); _cache.Insert(GetCacheKey(keyPair.Key), keyPair.Value, _cacheEvictionPolicyFunc(keyPair.Key)); } foreach (var keyPair in ResolveContentTypes(ds.Tables[1], CatalogContentType.CatalogNode)) { result.Add(keyPair.Key, keyPair.Value); _cache.Insert(GetCacheKey(keyPair.Key), keyPair.Value, _cacheEvictionPolicyFunc(keyPair.Key)); } return(result); }
public IEnumerable<IStatisticItem> GetStatisticItems(ContentReference rootContentReference) { var statisticItems = _synchronizedObjectInstanceCache.Get(CacheKey) as IEnumerable<IStatisticItem>; if (statisticItems != null) return statisticItems; statisticItems = _authorStatisticService.GetStatisticItems(rootContentReference); _synchronizedObjectInstanceCache.Insert(CacheKey, statisticItems, new CacheEvictionPolicy(new TimeSpan(0, 30, 0), CacheTimeoutType.Absolute)); return statisticItems; }
public void Set <TObj>(string key, TObj obj, TimeSpan timeSpan, CacheDurationType cacheDuration) { if (obj != null && !string.IsNullOrEmpty(key)) { var cacheEvictionPolicy = new CacheEvictionPolicy(timeSpan, _typeConveter[cacheDuration]); _syncronizedObjectInstanceCache.Insert(key, obj, cacheEvictionPolicy); _keyCache.Set(key, DateTime.Now.Add(timeSpan), timeSpan, cacheDuration); } }
public void Set(string key, object value, TimeSpan timeSpan) { _cache .Insert( key, value, new CacheEvictionPolicy( timeSpan, CacheTimeoutType.Sliding ) ); }
public void Insert(string key, object value, TimeSpan timeToLive) { if (String.IsNullOrWhiteSpace(key)) { return; } var evictionPolicy = new CacheEvictionPolicy(timeToLive, CacheTimeoutType.Absolute); _log.Debug(evictionPolicy, ep => $"Key: {key}, Item: {value}, CacheKeys: {ep?.CacheKeys}, Type: {ep?.TimeoutType}, Seconds: {ep?.Expiration.Duration().TotalSeconds}"); _cacheManager.Insert(key, value, evictionPolicy); }
public IEnumerable <ResolvedContentType> ResolveContentTypes( IEnumerable <ContentReference> contentLinks) { var result = GetFromCache(contentLinks, out var notCachedLinks); var internalResult = _internalResolver.ResolveContentTypes(notCachedLinks).ToList(); result.AddRange(internalResult); foreach (var resolved in internalResult) { _cache.Insert(GetCacheKey(resolved.ContentReference), resolved.ContentType, _cacheEvictionPolicyFunc(resolved.ContentReference)); } return(result); }
public NugetPackage GetCurrentFeedVersion(string packageId) { // We are calling out to an external service so cache results var cacheKey = "NugetFeedParser-Cache-" + packageId; if (_cache.Get("NugetFeedParser-Cache-" + packageId) != null) { var cacheVal = _cache.Get("NugetFeedParser-Cache-" + packageId); if (cacheVal.GetType() == typeof(NugetPackage)) { return((NugetPackage)cacheVal); } return(null); } var package = _packageRepository.FindPackage(packageId); if (package != null) { var nugetPackage = new NugetPackage() { Id = package.Id, PublishedDate = ((NuGet.DataServicePackage)(package)).LastUpdated.DateTime, Version = package.Version.Version.ToString(), FullSemanticVersion = package.Version }; _cache.Insert(cacheKey, nugetPackage, new CacheEvictionPolicy(null, null, null, new TimeSpan(1, 0, 0), CacheTimeoutType.Absolute)); return(nugetPackage); } //We do actually want to cache the fact we could not find the package on the Episerver Nuget repo, otherwise we'd be hitting the .FindPackage method every time _cache.Insert(cacheKey, false, new CacheEvictionPolicy(null, null, null, new TimeSpan(1, 0, 0), CacheTimeoutType.Absolute)); return(null); }
public BlogArticle GetItem(long id) { var article = _cache.Get <BlogArticle>($"{id}{CacheKeySuffix}", ReadStrategy.Wait); if (article != null) { return(article); } using (var client = new WebClient()) { var json = client.DownloadString(EktronPath("blogitemhandler", new List <KeyValuePair <string, object> >() { new KeyValuePair <string, object>("id", id) })); article = JsonConvert.DeserializeObject <BlogArticle>(json); if (article == null) { return(null); } _cache.Insert($"{id}{CacheKeySuffix}", article, new CacheEvictionPolicy(new TimeSpan(0, 30, 0), CacheTimeoutType.Absolute)); return(article); } }
protected virtual void Cache <TCache>(string cacheKey, TCache result) where TCache : class { _synchronizedObjectInstanceCache.Insert( cacheKey, result, new CacheEvictionPolicy(null, null, new[] { DataFactoryCache.RootKeyName, "EP:CatalogKeyPricesMasterCacheKey", "Mediachase.Commerce.InventoryService.Storage$MASTER" }, new TimeSpan(1, 0, 0), CacheTimeoutType.Sliding)); }
/// <summary> /// <summary>Inserts an item in the cache.</summary> /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expiration"></param> /// <param name="cacheKeys">The dependencies to other cached items, idetified by their keys.</param> /// <param name="masterKeys">The master keys that we depend upon. Master keys are used as markers to set up common dependencies without having to create the cache entries first. ///If you set up a master key dependency, there is no need for the corresponding entry to exist before adding ///something that depends on the master key. /// The concept of master keys could be regarded as similar to the cache region concept, but using master keys ///allows you to have more than one, where cache regions is restricted to one per cached item - you can only ///place the item in one region. /// </param> public void Insert(string key, object value, TimeSpan expiration, List <string> cacheKeys, List <string> masterKeys) { if (value == null) { return; } if (masterKeys == null) { masterKeys = new List <string>(); } if (cacheKeys == null) { cacheKeys = new List <string>(); } CacheEvictionPolicy cacheEvictionPolicy; if (!cacheKeys.Any() && !masterKeys.Any()) { cacheEvictionPolicy = new CacheEvictionPolicy(expiration, CacheTimeoutType.Absolute); } else if (masterKeys.Any() && !cacheKeys.Any()) { cacheEvictionPolicy = new CacheEvictionPolicy(expiration, CacheTimeoutType.Absolute, null, masterKeys.Select(k => $"{_keyPrefixDependency}{k}")); } else if (!masterKeys.Any() && cacheKeys.Any()) { cacheEvictionPolicy = new CacheEvictionPolicy(expiration, CacheTimeoutType.Absolute, cacheKeys.Select(k => $"{_keyPrefixDependency}{k}")); } else if (masterKeys.Any() && cacheKeys.Any()) { cacheEvictionPolicy = new CacheEvictionPolicy(expiration, CacheTimeoutType.Absolute, cacheKeys.Select(k => $"{_keyPrefixDependency}{k}"), masterKeys.Select(k => $"{_keyPrefixDependency}{k}")); } else { cacheEvictionPolicy = new CacheEvictionPolicy(expiration, CacheTimeoutType.Absolute); } var internalKey = _cacheKeyPrefix + key; _cacheManager.Insert(internalKey, value, cacheEvictionPolicy); }
public IEnumerable <RouteFacetModel> GetFacetModels() { var facetNames = GetCachedFacetNames(); if (facetNames != null) { return(facetNames); } var routingFacetNameStore = GetRoutingFacetNameStore(); var allRouteFacetModels = routingFacetNameStore.LoadAll <RouteFacetModel>(); var cacheKey = GetCacheName(); _objectInstanceCache.Insert(cacheKey, allRouteFacetModels, new CacheEvictionPolicy(new string[0])); return(allRouteFacetModels); }
private void ApplyFocalPointCropping(IUrlEventArgs urlEventArgs) { var focalPointData = urlResolver.Route(new UrlBuilder(urlEventArgs.VirtualPath)) as IFocalPointData; if (focalPointData?.FocalPoint != null) { var resizeSettings = GetResizeSettingsFromQueryString(urlEventArgs.QueryString); if (!ShouldCrop(focalPointData, resizeSettings)) { return; } Logger.Information($"Altering resize parameters for {focalPointData.Name} based on focal point."); var cacheKey = GetCacheKeyForResize(focalPointData.ContentLink, resizeSettings); var cropParameters = cache.Get(cacheKey) as string; if (cropParameters == null) { cropParameters = GetCropDimensions(focalPointData, resizeSettings).ToString(); cache.Insert(cacheKey, cropParameters, GetEvictionPolicy(focalPointData.ContentLink)); } urlEventArgs.QueryString.Add("crop", cropParameters); } }
/* * Each of the following simply returns the result of the default Episerver cache methods. * But illustrates how much can be overridden if, for example, you wanted to use a distributed cache provider. */ public void Insert(string key, object value, CacheEvictionPolicy evictionPolicy) => _defaultCache.Insert(key, value, evictionPolicy);