private void AddToCacheUrlContentNode(string cacheId, int cacheSeconds, string requestUrl, int nodeId, string template, bool forceTemplate) { // Add to the cache string cacheDependencyId = GetNodeCacheDependency(nodeId); var urlContentNode = new UrlContentNode() { Url = requestUrl, NodeId = nodeId, Template = template, ForceTemplate = forceTemplate }; Helpers.CacheHelper.GetExistingOrAddToCache(cacheId, cacheSeconds, CacheItemPriority.High, () => { return(urlContentNode); }, new[] { Constants.Config.ConfigFilePhysicalPath }, new[] { cacheDependencyId }); // Add to the persistent cache _PersistentCacheController.Add(urlContentNode); }
public void Remove(int nodeId, bool removePersistentCacheFile = false) { if (_PersistentCache == null) { LoadPersistentCache(); } if (_PersistentCache.Any()) { // Remove the items from the memory collection var itemsToRemove = _PersistentCache.Where(x => x.Value.NodeId == nodeId); int numRemovedItems = 0; foreach (var item in itemsToRemove) { // Try to remove the item int numAttempts = 0; UrlContentNode removedValue = null; while (!_PersistentCache.TryRemove(item.Key, out removedValue) && numAttempts < 10) { numAttempts++; System.Threading.Thread.Sleep(5); } //Check whether the item was removed successfully if (numAttempts < 10) { numRemovedItems++; } else { // Reset the persitent cache in order to avoid inconsistencies. ResetPersistentCache(); LogHelper.Warn <PersistentCacheController>(string.Format("Couldn't remove a UrlContentNode from the Routing persistent cache. In order to avoid inconsistencies the persitent cache was reset.")); return; } } // Delete the file only if the memory collection has changed if (removePersistentCacheFile && numRemovedItems > 0) { RemovePersistentCacheFile(); } } }
public void Add(UrlContentNode urlContentNode) { if (_PersistentCache == null) { LoadPersistentCache(); } // Setup the cache persistent update process in order to make sure that the urlContentNode that is going to be added/updated will be saved SetupPersistentCacheUpdateProcess(); // Add/Update the persistent cache _PersistentCache.AddOrUpdate(urlContentNode.Url, urlContentNode, (key, oldValue) => { oldValue.Url = urlContentNode.Url; oldValue.NodeId = urlContentNode.NodeId; oldValue.Template = urlContentNode.Template; oldValue.ForceTemplate = urlContentNode.ForceTemplate; return(oldValue); }); }