public string LockStringGet(ScdCacheKey cacheKey) { string lockName = "lockKey_" + cacheKey.ToString(); string lockToken = Guid.NewGuid().ToString(); if (AcquireLock(lockName, lockToken)) { try { var rValue = _db.StringGet(cacheKey.ToString()); if (!rValue.HasValue) { return(null); } var result = Deserialize <string>(rValue); return(result); } finally { _db.LockRelease(lockName, lockToken); } } else { return(null); } }
public void Set(ScdCacheKey key, object data, double cacheTime) { // 此处写入文件日志计算Redis的key以及操作对应的次数 var entryBytes = data == null ? new byte[0] : Serialize(data); XFileLoger.WriteLogEx("RedisNullCacheSet_" + key, CommonHelper.ConvertBytes(entryBytes.Length)); var expiresIn = TimeSpan.FromMinutes(cacheTime); _db.StringSet(key.ToString(), entryBytes, expiresIn); }
public void Remove(ScdCacheKey key) { var items = GetItems(); if (items == null) { return; } items.Remove(key.ToString()); }
public bool IsSet(ScdCacheKey key) { var items = GetItems(); if (items == null) { return(false); } return(items[key.ToString()] != null); }
public TResult Get <TResult>(ScdCacheKey key) { var items = GetItems(); if (items == null) { return(default(TResult)); } return((TResult)items[key.ToString()]); }
public void AddCache(ScdCacheKey key, object value, string dependencyFile) { if (!IsSet(key)) { CacheItemPolicy policy = new CacheItemPolicy(); List <string> filePath = new List <string>(); filePath.Add(dependencyFile); policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePath)); Cache.Set(key.ToString(), value, policy); } }
public void Set(ScdCacheKey key, object data, double cacheTime) { if (data == null) { return; } var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime); Cache.Add(new CacheItem(key.ToString(), data), policy); }
/// <summary> /// Get a cached item. If it's not in the cache yet, then load and cache it /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="cacheManager">Cache manager</param> /// <param name="key">Cache key</param> /// <param name="cacheTime">Cache time in minutes (0 - do not cache)</param> /// <param name="acquire">Function to load item if it's not in the cache yet</param> /// <returns>Cached item</returns> public static T Get <T>(this ICacheManager cacheManager, ScdCacheKey key, double cacheTime, Func <T> acquire) { if (cacheManager.IsSet(key)) { return(cacheManager.Get <T>(key)); } var result = acquire(); if (cacheTime > 0) { cacheManager.Set(key, result, cacheTime); } return(result); }
public TResult Get <TResult>(ScdCacheKey key) { var rValue = _db.StringGet(key.ToString()); if (!rValue.HasValue) { // 此处写入文件日志计算Redis的key以及操作对应的次数 XFileLoger.WriteLogEx("RedisCacheGet_" + key, "NoData"); return(default(TResult)); } // 此处写入文件日志计算Redis的key以及操作对应的次数 XFileLoger.WriteLogEx("RedisCacheGet_" + key, CommonHelper.ConvertBytes(((byte[])rValue).Length)); var result = Deserialize <TResult>(rValue); return(result); }
public void Set(ScdCacheKey key, object data, double cacheTime) { var items = GetItems(); if (items == null) { return; } if (data != null) { if (items.Contains(key.ToString())) { items[key.ToString()] = data; } else { items.Add(key.ToString(), data); } } }
public void LockStringSet(ScdCacheKey cacheKey, string vlaue) { string lockName = "lockKey_" + cacheKey.ToString(); string lockToken = Guid.NewGuid().ToString(); if (AcquireLock(lockName, lockToken)) { try { var entryBytes = Serialize(vlaue); var expiresIn = TimeSpan.FromMinutes(10); _db.StringSet(cacheKey.ToString(), entryBytes, expiresIn); } finally { _db.LockRelease(lockName, lockToken); } } }
public bool IsSet(ScdCacheKey key) { return(_db.KeyExists(key.ToString())); }
public void Remove(ScdCacheKey key) { _db.KeyDelete(key.ToString()); }
public bool IsSet(ScdCacheKey key) { return(Cache.Contains(key.ToString())); }
public void Remove(ScdCacheKey key) { Cache.Remove(key.ToString()); }
/// <summary> /// ScdCachekey /// </summary> /// <param name="keyFormat">keyFormart 如"site.user_{0}_{1}"</param> /// <param name="args">keyFormat的参数,可选</param> public static ScdCacheKey GetCacheKey(string keyFormat, params string[] args) { var cachekey = new ScdCacheKey(keyFormat, args); return(cachekey); }
public TResult Get <TResult>(ScdCacheKey key) { return((TResult)Cache[key.ToString()]); }
/// <summary> /// Get a cached item. If it's not in the cache yet, then load and cache it /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="cacheManager">Cache manager</param> /// <param name="key">Cache key</param> /// <param name="acquire">Function to load item if it's not in the cache yet</param> /// <returns>Cached item</returns> public static T Get <T>(this ICacheManager cacheManager, ScdCacheKey key, Func <T> acquire) { return(Get(cacheManager, key, 60, acquire)); }