/// <summary> /// 检查是否包括KEY值 /// </summary> /// <param name="key">KEY值</param> /// <returns></returns> public bool ContainsKey(string key) { if (!IsCache) { return(false); } key = KeyPrefix + key; return(CacheItemDictionary.ContainsKey(key)); }
/// <summary> /// 移除缓存 /// </summary> /// <param name="key">KEY值</param> public void Remove(string key) { if (!IsCache) { return; } key = KeyPrefix + key; CacheItemDictionary.Remove(key); }
/// <summary> /// 移除所有缓存 /// </summary> /// <returns>大于0表示移除的缓存记录数,等于0没有缓存可以移除。</returns> public int RemoveAll() { if (!IsCache) { return(0); } int interfaceCount = 0; interfaceCount = CacheItemDictionary.ItemDictionary.Count; CacheItemDictionary.Clear(); return(interfaceCount); }
/// <summary> /// 设置缓存 /// </summary> /// <param name="key">KEY值</param> /// <param name="value">对象值</param> /// <param name="expiryDate">过期时间</param> public void Set(string key, object value, DateTime expiryDate) { if (!IsCache) { return; } key = KeyPrefix + key; if (!value.GetType().IsSerializable) { throw new SerializationException("Object is not serializable"); } CacheItemDictionary.Add(key, new CacheItem(DateTime.Now, SerializationUtility.ToBytes(value), expiryDate)); }
/// <summary> /// 根据KEY获得缓存值 /// </summary> /// <param name="key">KEY值</param> /// <returns>查找到缓存值</returns> public object Get(string key) { if (!IsCache) { return(null); } key = KeyPrefix + key; if (CacheItemDictionary.ContainsKey(key)) { CacheItem cacheItem = CacheItemDictionary.Get(key); return(SerializationUtility.ToObject(cacheItem.value)); } else { return(null); } }