public object Add(string source, string cacheKey, object value) { lock (((ICollection)_cacheMap).SyncRoot) { if (_cacheMap.ContainsKey(source)) { if (log != null && log.IsDebugEnabled) { string msg = "Add to ASP.NET cache name: " + source + " key: " + cacheKey; log.Debug(msg); } CacheDescriptor cacheDescriptor = _cacheMap[source] as CacheDescriptor; if (!cacheDescriptor.SlidingExpiration) { return(HttpRuntime.Cache.Add(cacheKey, value, null, DateTime.Now.AddMinutes(cacheDescriptor.Timeout), TimeSpan.Zero, CacheItemPriority.Default, null)); } else { return(HttpRuntime.Cache.Add(cacheKey, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(cacheDescriptor.Timeout), CacheItemPriority.Default, null)); } } } if (log != null && log.IsDebugEnabled) { string msg = "Cannot add to ASP.NET cache the name: " + source + " key: " + cacheKey + ". Check your web.config file."; log.Debug(msg); } return(null); }
public object Add(string source, string cacheKey, object value) { string str; lock (((ICollection)this._cacheMap).SyncRoot) { if (this._cacheMap.ContainsKey(source)) { if ((log != null) && log.get_IsDebugEnabled()) { str = "Add to ASP.NET cache name: " + source + " key: " + cacheKey; log.Debug(str); } CacheDescriptor descriptor = this._cacheMap[source]; if (!descriptor.SlidingExpiration) { return(HttpRuntime.Cache.Add(cacheKey, value, null, DateTime.Now.AddMinutes((double)descriptor.Timeout), TimeSpan.Zero, CacheItemPriority.Normal, null)); } return(HttpRuntime.Cache.Add(cacheKey, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes((double)descriptor.Timeout), CacheItemPriority.Normal, null)); } } if ((log != null) && log.get_IsDebugEnabled()) { str = "Cannot add to ASP.NET cache the name: " + source + " key: " + cacheKey + ". Check your web.config file."; log.Debug(str); } return(null); }
public IEnumerable <ServiceCache> GetServiceCaches() { var cacheWrapperSetting = AppConfig.Configuration.Get <CachingProvider>(); var bingingSettings = cacheWrapperSetting.CachingSettings; var serviceCaches = new List <ServiceCache>(); foreach (var setting in bingingSettings) { var context = _serviceProvider.GetInstances <RedisContext>(setting.Id); foreach (var type in context.dicHash.Keys) { var cacheDescriptor = new CacheDescriptor { Id = $"{setting.Id}.{type.ToString()}", Prefix = setting.Id, Type = type }; int.TryParse(context.DefaultExpireTime, out int defaultExpireTime); int.TryParse(context.ConnectTimeout, out int connectTimeout); cacheDescriptor.DefaultExpireTime(defaultExpireTime); cacheDescriptor.ConnectTimeout(connectTimeout); var serviceCache = new ServiceCache { CacheDescriptor = cacheDescriptor, CacheEndpoint = context.dicHash[type].GetNodes() }; serviceCaches.Add(serviceCache); } } return(serviceCaches); }
public void AddCacheDescriptor(string name, int timeout, bool slidingExpiration) { CacheDescriptor cacheDescriptor = new CacheDescriptor(name, timeout, slidingExpiration); lock (((ICollection)_cacheMap).SyncRoot) { _cacheMap[name] = cacheDescriptor; } if (log != null && log.IsDebugEnabled) { string msg = "Loading key: " + name + " to cache map, timeout: " + timeout + " sliding expiration: " + slidingExpiration; log.Debug(msg); } }
public void AddCacheDescriptor(string name, int timeout, bool slidingExpiration) { CacheDescriptor descriptor = new CacheDescriptor(name, timeout, slidingExpiration); lock (((ICollection)this._cacheMap).SyncRoot) { this._cacheMap[name] = descriptor; } if ((log != null) && log.get_IsDebugEnabled()) { string str = string.Concat(new object[] { "Loading key: ", name, " to cache map, timeout: ", timeout, " sliding expiration: ", slidingExpiration }); log.Debug(str); } }
private void ParseCaches([NotNull] XElement root) { Debug.ArgumentNotNull(root, nameof(root)); var list = new List <CacheDescriptor>(); foreach (var element in root.Elements()) { var cacheDescriptor = new CacheDescriptor { Name = element.GetAttributeValue("name"), Size = element.GetAttributeLong("size", 0), Count = element.GetAttributeLong("count", 0), MaxSize = element.GetAttributeLong("maxsize", 0), Scavengable = element.GetAttributeValue("scavengable") == "true", Enabled = element.GetAttributeValue("enabled") == "true", Priority = element.GetAttributeValue("priority"), Delta = 0 }; var oldCache = _caches.FirstOrDefault(c => c.Name == cacheDescriptor.Name); if (oldCache != null) { cacheDescriptor.Delta = cacheDescriptor.Size - oldCache.Size; } else { cacheDescriptor.Delta = cacheDescriptor.Size; } list.Add(cacheDescriptor); } _caches.Clear(); _caches.AddRange(list); }
private static string GetKey(CacheDescriptor descriptor) { return(descriptor.Id); }
/// <summary> /// 设置连接超时时间。 /// </summary> /// <param name="descriptor">缓存描述述符。</param> /// <param name="connectTimeout">超时时间。</param> /// <returns>缓存描述符。</returns> public static CacheDescriptor ConnectTimeout(this CacheDescriptor descriptor, int connectTimeout) { descriptor.Metadatas["ConnectTimeout"] = connectTimeout; return(descriptor); }
/// <summary> /// 获取连接超时时间。 /// </summary> /// <param name="descriptor">缓存描述述符。</param> /// <returns>缓存描述符。</returns> public static int ConnectTimeout(this CacheDescriptor descriptor) { return(descriptor.GetMetadata("ConnectTimeout", 60)); }
/// <summary> /// 设置默认失效时间。 /// </summary> /// <param name="descriptor">缓存描述述符。</param> /// <param name="defaultExpireTime">The default expire time.</param> /// <returns>缓存描述符。</returns> public static CacheDescriptor DefaultExpireTime(this CacheDescriptor descriptor, int defaultExpireTime) { descriptor.Metadatas["DefaultExpireTime"] = defaultExpireTime; return(descriptor); }
/// <summary> /// 获取默认失效时间 。 /// </summary> /// <param name="descriptor">缓存描述符。</param> /// <returns>失效时间。</returns> public static int DefaultExpireTime(this CacheDescriptor descriptor) { return(descriptor.GetMetadata("DefaultExpireTime", 60)); }