/// <summary>
 /// 获取value
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public override object GetValue(string key)
 {
     try
     {
         CacheKeyMapDescriptor cacheKeyMap = EnsureKeyExist(key);
         OnOperating("开始获取value:starting:key:" + key + ",cacheId:" + cacheKeyMap.CacheId);
         if (cacheKeyMap == null)
         {
             return(null);
         }
         MongoDBCacheEntity entity = GetMongoDBEntity(cacheKeyMap);
         if (entity != null)
         {
             OnOperating("获取value成功:end-->:key:" + key + ",cacheId:" + cacheKeyMap.CacheId + ",value:" + entity.CacheValue);
             return(entity.CacheValue);
         }
         OnOperating("获取value失败:未找到key:" + key + "对应的值");
         return(null);
     }
     catch (Exception ex)
     {
         OnOperating("获取value异常:" + JsonConvert.SerializeObject(ex) + "");
         return(null);
     }
 }
 /// <summary>
 /// 确保key值存在
 /// </summary>
 /// <param name="key">key</param>
 /// <param name="isBackExpireData">如果当前key过期是否返回对应的对象</param>
 /// <returns></returns>
 private CacheKeyMapDescriptor EnsureKeyExist(string key, bool isBackExpireData = false)
 {
     try
     {
         CacheKeyMapDescriptor cacheKeyMap = CacheKeyMapManger.Instance.GetCacheKeyMap(key);
         if (cacheKeyMap == null)
         {
             return(null);
             // throw new KeyNotFoundException("Cache Key:" + key + " is not exist");
         }
         if (cacheKeyMap.ExpireDate <= DateTime.Now)
         {
             if (isBackExpireData)
             {
                 return(cacheKeyMap);
             }
             else
             {
                 return(null);
             }
         }
         return(cacheKeyMap);
         //if (!CacheKeyProvider.Instance.CacheKeyCollection.ContainsKey("_" + key))
         //{
         //    throw new KeyNotFoundException("Cache Key:" + key + " is not exist");
         //}
         //return CacheKeyProvider.Instance.CacheKeyCollection["_" + key];
     }
     catch (Exception ex)
     {
         OnOperating("EnsureKeyExist异常,excption:" + JsonConvert.SerializeObject(ex));
         return(null);
     }
 }
 /// <summary>
 /// 获取value
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public override bool TryGetValue <T>(string key, out T value)
 {
     try
     {
         T temp = default(T);
         CacheKeyMapDescriptor cacheKeyMap = EnsureKeyExist(key);
         OnOperating("开始尝试获取值:starting:key:" + key + ",cacheId:" + cacheKeyMap);
         if (cacheKeyMap == null)
         {
             value = default(T);
             return(false);
         }
         MongoDBCacheEntity entity = GetMongoDBEntity(cacheKeyMap);
         OnOperating("尝试获取值结束:end:key:" + key + ",cacheId:" + cacheKeyMap.CacheId + ",result:" + JsonConvert.SerializeObject(entity));
         value = default(T);
         value = JsonConvert.DeserializeObject <T>(entity.CacheValue);
         return(true);
     }
     catch (Exception ex)
     {
         OnOperating("尝试获取值异常:exception:key:" + key + ",excption:" + JsonConvert.SerializeObject(ex));
         value = default(T);
         return(false);
     }
 }
 private MongoDBCacheEntity GetMongoDBEntity(CacheKeyMapDescriptor cacheKeyMap)
 {
     try
     {
         MongoCollection <MongoDBCacheEntity> collection = DistributedCacheHelper.GetMongoDBCollection(cacheKeyMap.CacheId, cacheKeyMap.Cachelimit);
         var query2 = Query.And(
             Query.EQ("_id", cacheKeyMap.CacheId)
             //Query.GT("Expires", DateTime.Now),
             // Query.EQ("CacheSta", "1")
             //Query.EQ("ApplicationName", pApplicationName)
             );
         return(collection.FindOne(query2));
     }
     catch (Exception ex)
     {
         OnOperating("GetMongoDBEntity异常,excption:" + JsonConvert.SerializeObject(ex));
         return(null);
     }
 }
        /// <summary>
        /// 删除value
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override bool DeleteValue(string key, bool isClearOtherExpire)
        {
            try
            {
                CacheKeyMapDescriptor cacheKeyMap = EnsureKeyExist(key, true);
                if (cacheKeyMap == null)
                {
                    return(false);
                }
                OnOperating("开始删除值:starting:key:" + key + ",cacheId:" + cacheKeyMap.CacheId);
                MongoCollection <MongoDBCacheEntity> collection = DistributedCacheHelper.GetMongoDBCollection(cacheKeyMap.CacheId, cacheKeyMap.Cachelimit);
                var query2 = Query.And(
                    Query.EQ("_id", cacheKeyMap.CacheId)
                    );
                WriteConcernResult res = collection.Remove(query2);//移除文档
                //清空过期的集合数据

                if (res.Ok)
                {
                    bool r = CacheKeyMapManger.Instance.DeleteCacheKeyMap(key);
                    if (isClearOtherExpire)
                    {
                        DistributedCacheHelper.ClearExpireData(cacheKeyMap);
                    }
                    if (r)
                    {
                        OnOperating("删除值完成:end:key:" + key + ",cacheId:" + cacheKeyMap.CacheId + ",result:" + res.Ok + "," + res.ErrorMessage);
                        return(true);
                    }
                    OnOperating("删除值失败:end:key:" + key + ",cacheId:" + cacheKeyMap.CacheId + ",result:" + res.Ok + "," + res.ErrorMessage);
                    return(false);
                }
                OnOperating("删除值完成:end:key:" + key + ",cacheId:" + cacheKeyMap.CacheId + ",result:" + res.Ok + "," + res.ErrorMessage);
                return(false);
            }
            catch (Exception ex)
            {
                OnOperating("删除值异常:key:" + key + ",exception:" + JsonConvert.SerializeObject(ex));
                return(false);
            }
        }