public override List <CacheKeyMapDescriptor> GetCacheKeyMapList(Cachelimit cacheLimit, DateTime?startDate, DateTime?endDate) { List <CacheKeyMapDescriptor> lstMap = new List <CacheKeyMapDescriptor>(); try { MongoCollection <CacheKeyMapDescriptor> collection = DistributedCacheHelper.GetMongoDBCacheKeyMapCollection(); if (cacheLimit == Cachelimit.ByExpireDate) { var query = Query.And( Query.EQ("Cachelimit", cacheLimit), Query.EQ("ExpireDate", startDate), Query.EQ("ExpireDate", endDate) ); lstMap = collection.Find(query).ToList(); } else { var query = Query.And( Query.EQ("Cachelimit", cacheLimit) ); lstMap = collection.Find(query).ToList(); } return(lstMap); } catch (Exception ex) { return(lstMap); } }
///// <summary> /////设置value ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="key"></param> ///// <param name="value"></param> ///// <param name="expireDate"></param> //private override bool SetValue<T>(string key, T value, DateTime expireDate) //{ // try // { // string cacheId = CacheIdGeneratorManger.Instance.GenerateCacheId(); // OnOperating("开始写入值:starting:key:" + key + ",cacheId:" + cacheId + ",value:" + JsonConvert.SerializeObject(value) + ",expireDate:" + expireDate); // MongoDBCacheEntity cache = new MongoDBCacheEntity(); // cache.ApplicationName = this.pApplicationName; // cache.CacheId = cacheId; // cache.Created = DateTime.Now; // cache.CacheKey = key; // cache.ExpireDate = expireDate; // cache.CacheSta = CacheStatus.Effective; // cache.CacheValue = JsonConvert.SerializeObject(value); // MongoCollection<MongoDBCacheEntity> collection = DistributedCacheHelper.GetMongoDBCollection(cacheId); // if (collection != null) // { // try // { // var query = Query.And(Query.EQ("CacheKey", key)); // collection.Remove(query); // } // catch(Exception ex) // { } // WriteConcernResult res = collection.Save(cache); // OnOperating("写入完成end:key:" + key + ",cacheId:" + cacheId + ",result:" + res.Ok + "," + res.ErrorMessage); // if (res.Ok)//成功后 // { // CacheKeyMapManger.Instance.AddCacheKeyMap(key, cacheId, expireDate); // return true; // } // } // return false; // } // catch(Exception ex) // { // OnOperating("写入Excption:key:" + key + ",exception:" + JsonConvert.SerializeObject(ex)); // return false; // } //} /// <summary> /// 设置缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="cacheLimit"></param> /// <returns></returns> public override bool SetValue <T>(string key, T value, Cachelimit cacheLimit, DateTime?expireDate) { try { string cacheId = CacheIdGeneratorManger.Instance.GenerateCacheId(); OnOperating("开始写入值:starting:key:" + key + ",cacheId:" + cacheId + ",value:" + JsonConvert.SerializeObject(value) + ",cacheLimit:" + cacheLimit.ToString()); MongoDBCacheEntity cache = new MongoDBCacheEntity(); cache.ApplicationName = this.pApplicationName; cache.CacheId = cacheId; 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); if (cacheLimit == Cachelimit.ByExpireDate) { } MongoCollection <MongoDBCacheEntity> collection = DistributedCacheHelper.GetMongoDBCollection(cacheId, cacheLimit); if (collection != null) { try { var query = Query.And(Query.EQ("CacheKey", key)); collection.Remove(query); } catch (Exception ex) { } WriteConcernResult res = collection.Save(cache); 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 + ",cacheId:" + cacheId + ",result:" + res.Ok + "," + res.ErrorMessage); if (res.Ok)//成功后 { CacheKeyMapManger.Instance.AddCacheKeyMap(key, cacheId, expireDate, cacheLimit); return(true); } } return(false); } catch (Exception ex) { OnOperating("写入Excption:key:" + key + ",exception:" + JsonConvert.SerializeObject(ex)); return(false); } }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { Cachelimit limit = (Cachelimit)this.cbCacheLimit.SelectedIndex; if (limit == Cachelimit.ByExpireDate) { this.dmpExpireDate.Visible = true; this.label10.Visible = true; } else { this.dmpExpireDate.Visible = false; this.label10.Visible = false; } }
private void button1_Click(object sender, EventArgs e) { string key = this.txtKey.Text; string value = this.txtVal.Text; Cachelimit limit = (Cachelimit)this.cbCacheLimit.SelectedIndex; DateTime? expireDate = null; if (limit == Cachelimit.ByExpireDate) { expireDate = this.dmpExpireDate.Value; } bool r = CacheManger.Instance.SetValue(key, value, limit, expireDate); this.button4.PerformClick(); if (r) { MessageBox.Show("写入成功"); return; } MessageBox.Show("写入失败"); }
public override List <CacheKeyMapDescriptor> GetCacheKeyMapList(Cachelimit cacheLimit, DateTime?startDate, DateTime?endDate) { List <CacheKeyMapDescriptor> lstMap = new List <CacheKeyMapDescriptor>(); try { if (cacheLimit == Cachelimit.ByExpireDate) { lstMap = mapList.ToList().Where(x => x.Cachelimit == cacheLimit && x.ExpireDate == startDate && x.ExpireDate == endDate).ToList(); } else { lstMap = mapList.ToList().Where(x => x.Cachelimit == cacheLimit).ToList(); } return(lstMap); } catch (Exception ex) { return(lstMap); } }
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); }
public override bool SetValue <T>(string key, T value, Cachelimit cacheLimit, DateTime?expireDate = null) { return(_cache.SetValue <T>(key, value, cacheLimit, expireDate)); }
public bool SetValue <T>(string key, T value, Cachelimit cacheLimit, DateTime?expireDate = null) where T : class { return(_cache.SetValue <T>(key, value, cacheLimit, expireDate)); }
public override List <CacheKeyMapDescriptor> GetCacheKeyMapList(Cachelimit cacheLimit, DateTime?startDate, DateTime?endDate) { return(_cacheKeyMap.GetCacheKeyMapList(cacheLimit, startDate, endDate)); }
public override void AddCacheKeyMap(string cacheKey, string cacheId, DateTime?expireDate, Cachelimit cacheLimit) { _cacheKeyMap.AddCacheKeyMap(cacheKey, cacheId, expireDate, cacheLimit); }
public abstract bool SetValue <T>(string key, T value, Cachelimit cacheLimit, DateTime?expireDate) where T : class;
public abstract List <CacheKeyMapDescriptor> GetCacheKeyMapList(Cachelimit cacheLimit, DateTime?startDate, DateTime?endDate);
public virtual void AddCacheKeyMap(string cacheKey, string cacheId, DateTime?expireDate, Cachelimit cacheLimit) { DateTime dt = DateTime.Now; switch (cacheLimit) { case Cachelimit.ByExpireDate: dt = Convert.ToDateTime(expireDate); break; case Cachelimit.CurrentDay: dt = Convert.ToDateTime(dt.ToString("yyyy-MM-dd") + " 23:59:59"); break; case Cachelimit.Forever: dt = Convert.ToDateTime("2099-12-30"); break; default: break; } AddCacheKeyMap(new CacheKeyMapDescriptor() { CacheId = cacheId, CacheKey = cacheKey, Cachelimit = cacheLimit, CacheSta = CacheStatus.Effective, ExpireDate = dt }); }
public static MongoCollection <MongoDBCacheEntity> GetMongoDBCollection(string cacheId, Cachelimit cacheLimit) { IPartitionResolver resolver = new MongoDBCachePartitionResolver(); string mongoDbConnectionString = resolver.ResolvePartition(cacheId); MongoClient client = new MongoClient(mongoDbConnectionString); MongoServer srv = client.GetServer(); MongoDatabase db = srv.GetDatabase(MongoDBCacheConfiguration.DBName); string tableName = cacheLimit == Cachelimit.Forever ? MongoDBCacheConfiguration.TableName + "Forever" : MongoDBCacheConfiguration.TableName + "ByExpireDate"; if (!db.CollectionExists(tableName)) { db.CreateCollection(tableName); } MongoCollection <MongoDBCacheEntity> collection = db.GetCollection <MongoDBCacheEntity>(tableName); return(collection); }
public bool SetValue <T>(string key, T value, Cachelimit cacheLimit = Cachelimit.CurrentDay, DateTime?expireDate = null) where T : class { return(false); }