Exemple #1
0
        /// <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));
        }
Exemple #2
0
 /// <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);
     }
 }