/// <summary> /// Formats the concatenation of the cache tag /// </summary> /// <param name="type"></param> /// <param name="identifier"></param> /// <returns></returns> public static string FormatTag(CacheTags type, dynamic identifier) { if (identifier == null) { identifier = "null"; } string id = type.ToString() + identifier.ToString(); return(id); }
public string GetUrlForFileAttribute(int fieldId) { return(_cacheProvider.GetOrAdd($"DBConnectorProxy.GetUrlForFileAttribute_{fieldId}", CacheTags.Merge(CacheTags.QP8.Site, CacheTags.QP8.Field), TimeSpan.FromMinutes(10), () => { return GetConnector().GetUrlForFileAttribute(fieldId); })); }
/// <summary>Expire all cached keys linked to specified tags.</summary> /// <param name="tags"> /// A variable-length parameters list containing tag to expire linked cache /// key. /// </param> public static void ExpireTag(params string[] tags) { foreach (var tag in tags) { List <string> list; if (CacheTags.TryRemove(CachePrefix + tag, out list)) { foreach (var item in list) { Cache.Remove(item); } } } }
/// <summary>Adds cache tags corresponding to a cached key in the CacheTags dictionary.</summary> /// <param name="cacheKey">The cache key.</param> /// <param name="tags">A variable-length parameters list containing tags corresponding to the <paramref name="cacheKey" />.</param> internal static void AddCacheTag(string cacheKey, params string[] tags) { foreach (var tag in tags) { CacheTags.AddOrUpdate(CachePrefix + tag, x => new List <string> { cacheKey }, (x, list) => { if (!list.Contains(cacheKey)) { list.Add(cacheKey); } return(list); }); } }
/// <summary>Expire all cached keys linked to specified tags.</summary> /// <param name="tags"> /// A variable-length parameters list containing tag to expire linked cache /// key. /// </param> public static void ExpireTag(params string[] tags) { foreach (var tag in tags) { List <string> list; if (CacheTags.TryRemove(CachePrefix + tag, out list)) { // never lock something related to this list elsewhere or ensure we don't create a deadlock lock (list) { foreach (var item in list) { Cache.Remove(item); } } } } }
/// <summary>Adds cache tags corresponding to a cached key in the CacheTags dictionary.</summary> /// <param name="cacheKey">The cache key.</param> /// <param name="tags">A variable-length parameters list containing tags corresponding to the <paramref name="cacheKey" />.</param> internal static void AddCacheTag(string cacheKey, params string[] tags) { foreach (var tag in tags) { CacheTags.AddOrUpdate(CachePrefix + tag, x => new List <string> { cacheKey }, (x, list) => { lock (list) { // never lock something related to this list elsewhere or ensure we don't create a deadlock if (!list.Contains(cacheKey)) { list.Add(cacheKey); } } return(list); }); } }
/// <summary>Expire all cached objects && tag.</summary> public static void ExpireAll() { var tags = CacheTags.Select(x => x.Key).ToList(); // We do not use ExpireTag because type doesn't have CachePrefix foreach (var tag in tags) { List <string> list; if (CacheTags.TryRemove(tag, out list)) { // never lock something related to this list elsewhere or ensure we don't create a deadlock lock (list) { foreach (var item in list) { Cache.Remove(item); } } } } }
public static void demo_cache3() { DbContext db = DbConfig.pc_bcf; ICacheServiceEx cache = null; //1.缓存并添加简易标签 db.call("user_get").set("xxx", 1) .caching(cache) .cacheTag("user_" + 1) .usingCache(60 * 1000) .getItem(new UserInfoModel()); CacheTags tags = new CacheTags(cache); //2.1.可根据标签清除缓存 tags.clear("user_" + 1); //2.2.可根据标签更新缓存 tags.update <UserInfoModel>("user_" + 1, (m) => { m.name = "xxx"; return(m); }); }
/// <summary> /// Clears all the items with this tag /// </summary> /// <param name="tag"></param> public void ClearTag(CacheTags cacheTag, dynamic id) { if (id == null) { id = ""; } string tag = cacheTag.ToString() + id; var keysToRemove = new List <string>(); foreach (DictionaryEntry item in System.Web.HttpRuntime.Cache) { if (!(item.Value is ICachable)) { continue; } var key = (ICachable)item.Value; if (key.Tags.Contains(tag)) { keysToRemove.Add(item.Key.ToString()); } } // Remove each foreach (var cacheKey in keysToRemove) { try { if (System.Web.HttpRuntime.Cache[cacheKey] != null) { System.Web.HttpRuntime.Cache.Remove(cacheKey); } } catch { } } }
/// <summary> /// Assigns this identifier to the tags property of the cache item /// </summary> /// <param name="cache"></param> /// <param name="type"></param> /// <param name="identifier"></param> public static void AddTag(this ICachable cache, CacheTags type, dynamic identifier) { cache.Tags.Add(FormatTag(type, identifier)); }
public void ClearTag(CacheTags tag, dynamic id) { }