Example #1
0
 public override bool TryGetValue <T>(string key, out T value)
 {
     try
     {
         MongoDBCacheEntity entity = GetMongoDBEntity(key);
         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);
     }
 }
Example #2
0
 public override object GetValue(string key)
 {
     try
     {
         MongoDBCacheEntity entity = GetMongoDBEntity(key);
         if (entity != null)
         {
             OnOperating("获取value成功:end-->:key:" + key + ",value:" + entity.CacheValue);
             return(entity.CacheValue);
         }
         OnOperating("获取value失败:未找到key:" + key + "对应的值");
         return(null);
     }
     catch (Exception ex)
     {
         OnOperating("获取value异常:" + JsonConvert.SerializeObject(ex) + "");
         return(null);
     }
 }
Example #3
0
        public override bool SetValue <T>(string key, T value, Cachelimit cacheLimit, DateTime?expireDate)
        {
            Server             server = ChooseServer(key);
            MongoDBCacheEntity cache  = new MongoDBCacheEntity();

            cache.ApplicationName = "";
            cache.Created         = DateTime.Now;
            cache.CacheKey        = key;
            cache.ExpireDate      = cacheLimit == Cachelimit.Forever ? Convert.ToDateTime("2099-12-30") : (cacheLimit == Cachelimit.CurrentDay ? Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59") : Convert.ToDateTime(expireDate));
            cache.CacheSta        = CacheStatus.Effective;


            cache.CacheValue = JsonConvert.SerializeObject(value);

            MongoCollection <MongoDBCacheEntity> collection = GetMongoDBCollection(server.ConnStr);

            if (collection != null)
            {
                try
                {
                    var query = Query.And(Query.EQ("CacheKey", key));
                    collection.Remove(query);
                }
                catch (Exception ex)
                { }
                var document                = BsonSerializer.Deserialize <BsonDocument>(JsonConvert.SerializeObject(cache));
                WriteConcernResult res      = collection.Save(document);
                TimeSpan           timespan = cache.ExpireDate.Subtract(DateTime.Now);
                if (cacheLimit != Cachelimit.Forever)
                {
                    if (!collection.IndexExists(new IndexKeysBuilder().Ascending("ExpireDate")))
                    {
                        collection.EnsureIndex(new IndexKeysBuilder().Ascending("ExpireDate"), IndexOptions.SetTimeToLive(timespan));
                    }
                }
                OnOperating("写入完成end:key:" + key + ",serverConnstr:" + server.connstr + ",result:" + res.Ok + "," + res.ErrorMessage);
                return(true);
            }
            return(false);
        }