public void CanValidateWhetherObjectIsCached() { _staticCacheManager.Set(new CacheKey("some_key_1"), 3); _staticCacheManager.Set(new CacheKey("some_key_2"), 4); _staticCacheManager.IsSet(new CacheKey("some_key_1")).Should().BeTrue(); _staticCacheManager.IsSet(new CacheKey("some_key_3")).Should().BeFalse(); }
public void Can_validate_whetherobject_is_cached() { _cacheManager.Set("some_key_1", 3, int.MaxValue); _cacheManager.Set("some_key_2", 4, int.MaxValue); _cacheManager.IsSet("some_key_1").ShouldEqual(true); _cacheManager.IsSet("some_key_3").ShouldEqual(false); }
public void Can_validate_whetherobject_is_cached() { _cacheManager.Set(new CacheKey("some_key_1"), 3); _cacheManager.Set(new CacheKey("some_key_2"), 4); _cacheManager.IsSet(new CacheKey("some_key_1")).Should().BeTrue(); _cacheManager.IsSet(new CacheKey("some_key_3")).Should().BeFalse(); }
public void Can_validate_whetherobject_is_cached() { var cacheManager = new MemoryCacheManager(); cacheManager.Set("some_key_1", 3, int.MaxValue); cacheManager.Set("some_key_2", 4, int.MaxValue); cacheManager.IsSet("some_key_1").ShouldEqual(true); cacheManager.IsSet("some_key_3").ShouldEqual(false); }
public async Task IsSetTest() { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions { })); await memoryCacheManager.SetAsync("exampleKey05", 0, int.MaxValue); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey05")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey08")); }
public void IsSetTest() { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(); memoryCacheManager.Set("exampleKey05", 0, int.MaxValue); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey05")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey08")); }
public void Can_validate_whetherobject_is_cached() { var cacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions())); cacheManager.Set("some_key_1", 3, int.MaxValue); cacheManager.Set("some_key_2", 4, int.MaxValue); Assert.True(cacheManager.IsSet("some_key_1") == true); Assert.True(cacheManager.IsSet("some_key_2") == true); }
public void Can_validate_whetherobject_is_cached() { var cacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions())); cacheManager.Set("some_key_1", 3, int.MaxValue); cacheManager.Set("some_key_2", 4, int.MaxValue); cacheManager.IsSet("some_key_1").ShouldEqual(true); cacheManager.IsSet("some_key_3").ShouldEqual(false); }
public void removing_one_item_of_Cache() { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(); memoryCacheManager.Set("exampleKey15", 5, int.MaxValue); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey15")); memoryCacheManager.Remove("exampleKey15"); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey15")); }
public void Can_validate_cached_object() { var cacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions())); cacheManager.Set("key_1", 1, int.MaxValue); cacheManager.Set("key_2", 2, int.MaxValue); cacheManager.IsSet("key_1").ShouldEqual(true); cacheManager.IsSet("key_3").ShouldEqual(false); }
public async Task Removing_one_item_of_Cache() { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions { })); await memoryCacheManager.SetAsync("exampleKey15", 5, int.MaxValue); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey15")); await memoryCacheManager.RemoveAsync("exampleKey15"); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey15")); }
public async Task IsSetTest() { var eventPublisher = new Mock <IMediator>(); MemoryCacheManager memoryCacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions { }), eventPublisher.Object); await memoryCacheManager.SetAsync("exampleKey05", 0, int.MaxValue); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey05")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey08")); }
public async Task Removing_one_item_of_Cache() { var eventPublisher = new Mock <IMediator>(); MemoryCacheManager memoryCacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions { }), eventPublisher.Object); await memoryCacheManager.SetAsync("exampleKey15", 5, int.MaxValue); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey15")); await memoryCacheManager.RemoveAsync("exampleKey15"); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey15")); }
public void CanValidateWhetherObjectIsCached() { var cacheManager = new MemoryCacheManager(); cacheManager.Clear(); cacheManager.Set("some_key_1", 3, int.MaxValue); cacheManager.Set("some_key_2", 4, int.MaxValue); cacheManager.IsSet("some_key_1").ShouldEqual(true); cacheManager.IsSet("some_key_2").ShouldEqual(true); cacheManager.IsSet("some_key_3").ShouldEqual(false); }
public void clearing_whole_Cache() { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(); memoryCacheManager.Set("exampleKey25", 5, int.MaxValue); memoryCacheManager.Set("exampleKey35", 5, int.MaxValue); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey25")); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey35")); memoryCacheManager.Clear(); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey25")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey35")); }
/// <summary> /// 获取对象的属性对象 /// 本方法提供缓存功能, 提高性能 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="propertyName"></param> /// <returns></returns> public static PropertyInfo GetProperty <TEntity>(string propertyName) { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(); Dictionary <string, PropertyInfo> propertyDictionary = null; string cachingKey = PropertyCollectionPreKey + typeof(TEntity).Name; if (!memoryCacheManager.IsSet(cachingKey)) { propertyDictionary = new Dictionary <string, PropertyInfo>(); Type type = typeof(TEntity); PropertyInfo[] propertyInfos = type.GetProperties(); foreach (PropertyInfo property in propertyInfos) { propertyDictionary.Add(property.Name, property); } memoryCacheManager.Set(cachingKey, propertyDictionary, 60); } propertyDictionary = memoryCacheManager.Get <Dictionary <string, PropertyInfo> >(cachingKey); if (propertyDictionary.ContainsKey(propertyName)) { return(propertyDictionary[propertyName]); } return(null); }
public void Can_lock_cache() { var cacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions())); var key = ".Task"; var expiration = TimeSpan.FromMinutes(2); var actionCount = 0; var action = new Action(() => { cacheManager.IsSet(key).ShouldBeTrue(); cacheManager.PerformActionWithLock(key, expiration, () => Assert.Fail("Action in progress")).ShouldBeFalse(); if (++actionCount % 2 == 0) { throw new ApplicationException("Alternating actions fail"); } }); cacheManager.PerformActionWithLock(key, expiration, action).ShouldBeTrue(); actionCount.ShouldEqual(1); Assert.Throws <ApplicationException>(() => cacheManager.PerformActionWithLock(key, expiration, action)); actionCount.ShouldEqual(2); cacheManager.PerformActionWithLock(key, expiration, action).ShouldBeTrue(); actionCount.ShouldEqual(3); }
public async Task Clearing_whole_Cache() { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions { })); await memoryCacheManager.SetAsync("exampleKey25", 5, int.MaxValue); await memoryCacheManager.SetAsync("exampleKey35", 5, int.MaxValue); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey25")); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey35")); await memoryCacheManager.Clear(); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey25")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey35")); }
public void Can_clear_cache() { var cacheManager = new MemoryCacheManager(); cacheManager.Set("some_key_1", 3, int.MaxValue); cacheManager.Clear(); cacheManager.IsSet("some_key_1").ShouldEqual(false); }
public async Task Clearing_whole_Cache() { var eventPublisher = new Mock <IMediator>(); MemoryCacheManager memoryCacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions { }), eventPublisher.Object); await memoryCacheManager.SetAsync("exampleKey25", 5, int.MaxValue); await memoryCacheManager.SetAsync("exampleKey35", 5, int.MaxValue); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey25")); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey35")); await memoryCacheManager.Clear(); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey25")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey35")); }
public void Can_clear_cache() { var cacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions())); cacheManager.Set("key_1", 1, int.MaxValue); cacheManager.Clear(); cacheManager.IsSet("key_1").ShouldEqual(false); }
public void RemoveByPatternTest() { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(); memoryCacheManager.Set("exampleKey025", 5, int.MaxValue); memoryCacheManager.Set("exampleKey026", 5, int.MaxValue); memoryCacheManager.Set("exampleKey027", 5, int.MaxValue); memoryCacheManager.Set("exampleKey127", 5, int.MaxValue); string pattern = @"exampleKey0\d\d"; memoryCacheManager.RemoveByPattern(pattern); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey025")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey026")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey027")); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey127")); }
public void Can_clear_cache() { var cacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions())); cacheManager.Set("some_key_1", 3, int.MaxValue); cacheManager.Clear(); Assert.True(cacheManager.IsSet("some_key_1") == false); }
public async Task RemoveByPatternTest() { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions { })); await memoryCacheManager.SetAsync("exampleKey025", 5, int.MaxValue); await memoryCacheManager.SetAsync("exampleKey026", 5, int.MaxValue); await memoryCacheManager.SetAsync("exampleKey027", 5, int.MaxValue); await memoryCacheManager.SetAsync("exampleKey127", 5, int.MaxValue); string pattern = @"exampleKey0\d\d"; await memoryCacheManager.RemoveByPattern(pattern); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey025")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey026")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey027")); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey127")); }
/// <summary> /// 获取类型属性 /// 增加缓存机制 /// </summary> /// <param name="target"></param> /// <returns></returns> public static PropertyInfo[] GetPropertys(object target) { MemoryCacheManager memoryCacheManager = new MemoryCacheManager(); string cachingKey = PropertyCollectionPreKey + target.GetType().Name; if (!memoryCacheManager.IsSet(cachingKey)) { Type type = target.GetType(); PropertyInfo[] propertyInfos = type.GetProperties(); memoryCacheManager.Set(cachingKey, propertyInfos, _cacheMinuters); } return(memoryCacheManager.Get <PropertyInfo[]>(cachingKey)); }
public async Task RemoveByPrefixTest() { var eventPublisher = new Mock <IMediator>(); MemoryCacheManager memoryCacheManager = new MemoryCacheManager(new MemoryCache(new MemoryCacheOptions { }), eventPublisher.Object); await memoryCacheManager.SetAsync("exampleKey025", 5, int.MaxValue); await memoryCacheManager.SetAsync("exampleKey026", 5, int.MaxValue); await memoryCacheManager.SetAsync("exampleKey027", 5, int.MaxValue); await memoryCacheManager.SetAsync("exampleKey127", 5, int.MaxValue); string pattern = @"exampleKey0"; await memoryCacheManager.RemoveByPrefix(pattern); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey025")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey026")); Assert.IsFalse(memoryCacheManager.IsSet("exampleKey027")); Assert.IsTrue(memoryCacheManager.IsSet("exampleKey127")); }
/// <summary> /// 根据用户UID初始化用户权限对象并设置全局缓存 /// </summary> /// <param name="uid"></param> private static void SettingLimits(string uid) { var ukey = uid + "_" + key_limitId; //取权限 var roleBLL = new SysRoleBLL(); var objs = roleBLL.GetRoleLimitsByUId(uid); //设置缓存 var cacheManager = new MemoryCacheManager(); if (cacheManager.IsSet(ukey)) { cacheManager.Remove(ukey); } cacheManager.Set(ukey, objs, 1440); }
/// <summary> /// Gets the value associated with the specified key. /// </summary> /// <typeparam name="T">Type of cached item</typeparam> /// <param name="key">Key of cached item</param> /// <returns>The cached value associated with the specified key</returns> protected virtual async Task <T> GetAsync <T>(CacheKey key) { //little performance workaround here: //we use "MemoryCacheManager" to cache a loaded object in memory for the current HTTP request. //this way we won't connect to Redis server many times per HTTP request (e.g. each time to load a locale or setting) if (_memoryCacheManager.IsSet(key)) { return(_memoryCacheManager.Get(key, () => default(T))); } //get serialized item from cache var serializedItem = await _db.StringGetAsync(key.Key); if (!serializedItem.HasValue) { return(default);
/// <summary> /// 插入必填字段信息 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="action"></param> /// <param name="entity"></param> internal void Injection <TEntity>(OpreateActionType action, TEntity entity) where TEntity : BaseEntity { MemoryCacheManager manager = new MemoryCacheManager(); string key = typeof(TEntity).FullName + action.ToString(); List <PropertyInfo> list = null; if (!manager.IsSet(key)) { #region 增加缓存 if (list == null || list.Count < 0) { PropertyInfo[] propertyList = typeof(TEntity).GetProperties(); list = new List <PropertyInfo>(); propertyList.Each(c => { if (action == OpreateActionType.INSERT) { //c.Name == "ID" || if (c.Name == "DateCreated" || c.Name == "DateModified" || c.Name == "Version") { list.Add(c); } } if (action == OpreateActionType.UPDATE) { if (c.Name == "DateModified" || c.Name == "Version") { list.Add(c); } } if (c.PropertyType.IsGenericType) { list.Add(c); } }); manager.Set(key, list, 60); } #endregion } list = manager.Get <List <PropertyInfo> >(key); foreach (PropertyInfo field in list) { //if (field.Name == "ID") //{ // field.SetValue(entity, Guid.NewGuid().ToString("N"), null); //} if (field.Name == "DateCreated") { field.SetValue(entity, DateTime.Now, null); } if (field.Name == "DateModified") { field.SetValue(entity, DateTime.Now, null); } if (field.Name == "Version") { field.SetValue(entity, DateTime.UtcNow.Ticks.ToString(), null); } } }
/// <summary> /// 通过查询条件获取实体集 /// </summary> /// <param name="filter">查询条件</param> /// <param name="orderby">排序</param> /// <returns></returns> public static List <T> GetEntityList(string filter = "", string orderby = "", bool isDefaultCance = false) { IList <T> ilist = new List <T>(); T _t = (T)Activator.CreateInstance(typeof(T)); filter = string.IsNullOrWhiteSpace(filter) ? " where 1=1 " : " where " + filter; #region 判断是否有删除标记,过滤删除数据 PropertyInfo[] propertys = _t.GetType().GetProperties(); if (propertys.Where(f => f.Name.ToUpper().Equals("DEL")).Count() > 0) { if (!filter.ToUpper().Contains("DEL") && !isDefaultCance) { filter += " and nvl(DEL,0) <> 1 "; } } if (propertys.Where(f => f.Name.ToUpper().Equals("ISCANCE")).Count() > 0) { if (!filter.ToUpper().Contains("ISCANCE") && !isDefaultCance) { filter += " and nvl(ISCANCE,0) <> 1 "; } } #endregion string strSQL = "select * from " + _t.GetType().Name + filter; if (orderby != "") { strSQL += " order by " + orderby; } //根据每个实体的CacheType属性,进行数据缓存,如果未设置该属性的,则不缓存 if (_t.GetType().GetCustomAttribute <CacheTypeAttribute>() != null) { ICacheManager cacheManager = null; switch (_t.GetType().GetCustomAttribute <CacheTypeAttribute>().cacheType) { case CacheTypeEnum.Redis: cacheManager = RedisCacheManager.CreateInstance(); break; case CacheTypeEnum.MemoryCache: cacheManager = new MemoryCacheManager(); break; } if (cacheManager.IsSet(_t.GetType().Name)) { ilist = cacheManager.Get <List <T> >(_t.GetType().Name); } else { ilist = DataSetToEntityList(dataProvider.GetDataSet(strSQL), 0); cacheManager.Set(_t.GetType().Name, ilist, 60); } } else { ilist = DataSetToEntityList(dataProvider.GetDataSet(strSQL), 0); } if (ilist != null && ilist.Count() > 0) { return(ilist.ToList()); } else { return(new List <T>()); } }