日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

个人项目框架搭建 -- 缓存接口与实现

發布時間:2025/5/22 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 个人项目框架搭建 -- 缓存接口与实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、緩存接口

using System; using System.Collections.Generic; using System.Runtime.Caching; using System.Text.RegularExpressions;namespace EnterpriseFrame.Core.Caching {/// <summary>/// Represents a manager for caching between HTTP requests (long term caching)/// </summary>public partial class MemoryCacheManager : ICacheManager{protected ObjectCache Cache{get{return MemoryCache.Default;}}/// <summary>/// Gets or sets the value associated with the specified key./// </summary>/// <typeparam name="T">Type</typeparam>/// <param name="key">The key of the value to get.</param>/// <returns>The value associated with the specified key.</returns>public virtual T Get<T>(string key){return (T)Cache[key];}/// <summary>/// Adds the specified key and object to the cache./// </summary>/// <param name="key">key</param>/// <param name="data">Data</param>/// <param name="cacheTime">Cache time</param>public virtual void Set(string key, object data, int cacheTime){if (data == null)return;var policy = new CacheItemPolicy();policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);Cache.Add(new CacheItem(key, data), policy);}/// <summary>/// Gets a value indicating whether the value associated with the specified key is cached/// </summary>/// <param name="key">key</param>/// <returns>Result</returns>public virtual bool IsSet(string key){return (Cache.Contains(key));}/// <summary>/// Removes the value with the specified key from the cache/// </summary>/// <param name="key">/key</param>public virtual void Remove(string key){Cache.Remove(key);}/// <summary>/// Removes items by pattern/// </summary>/// <param name="pattern">pattern</param>public virtual void RemoveByPattern(string pattern){var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);var keysToRemove = new List<String>();foreach (var item in Cache)if (regex.IsMatch(item.Key))keysToRemove.Add(item.Key);foreach (string key in keysToRemove){Remove(key);}}/// <summary>/// Clear all cache data/// </summary>public virtual void Clear(){foreach (var item in Cache)Remove(item.Key);}} }

2、擴展

using System;namespace EnterpriseFrame.Core.Caching {/// <summary>/// Extensions/// </summary>public static class CacheExtensions{/// <summary>/// 獲取緩存,不存在則將acquire的結果存入緩存,默認時間60分鐘/// </summary>/// <typeparam name="T">Type</typeparam>/// <param name="cacheManager">Cache manager</param>/// <param name="key">Cache key</param>/// <param name="acquire">Function to load item if it's not in the cache yet</param>/// <returns>Cached item</returns>public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire){return Get(cacheManager, key, 60, acquire);}/// <summary>/// 獲取緩存,如果不存在緩存中,執行結果然后將其加載和緩存/// </summary>/// <typeparam name="T">緩存類型</typeparam>/// <param name="cacheManager">Cache manager</param>/// <param name="key">Cache key</param>/// <param name="cacheTime">緩存時間 在0分鐘-不要緩存</param>/// <param name="acquire">沒有緩存則執行此表達式設置緩存</param>/// <returns>Cached item</returns>public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, Func<T> acquire) {if (cacheManager.IsSet(key)){return cacheManager.Get<T>(key);}else{var result = acquire();if (cacheTime > 0)cacheManager.Set(key, result, cacheTime);return result;}}} }

?

3、MemoryCacheManager實現

using System; using System.Collections.Generic; using System.Runtime.Caching; using System.Text.RegularExpressions;namespace EnterpriseFrame.Core.Caching {/// <summary>/// Represents a manager for caching between HTTP requests (long term caching)/// </summary>public partial class MemoryCacheManager : ICacheManager{protected ObjectCache Cache{get{return MemoryCache.Default;}}/// <summary>/// Gets or sets the value associated with the specified key./// </summary>/// <typeparam name="T">Type</typeparam>/// <param name="key">The key of the value to get.</param>/// <returns>The value associated with the specified key.</returns>public virtual T Get<T>(string key){return (T)Cache[key];}/// <summary>/// Adds the specified key and object to the cache./// </summary>/// <param name="key">key</param>/// <param name="data">Data</param>/// <param name="cacheTime">Cache time</param>public virtual void Set(string key, object data, int cacheTime){if (data == null)return;var policy = new CacheItemPolicy();policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);Cache.Add(new CacheItem(key, data), policy);}/// <summary>/// Gets a value indicating whether the value associated with the specified key is cached/// </summary>/// <param name="key">key</param>/// <returns>Result</returns>public virtual bool IsSet(string key){return (Cache.Contains(key));}/// <summary>/// Removes the value with the specified key from the cache/// </summary>/// <param name="key">/key</param>public virtual void Remove(string key){Cache.Remove(key);}/// <summary>/// Removes items by pattern/// </summary>/// <param name="pattern">pattern</param>public virtual void RemoveByPattern(string pattern){var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);var keysToRemove = new List<String>();foreach (var item in Cache)if (regex.IsMatch(item.Key))keysToRemove.Add(item.Key);foreach (string key in keysToRemove){Remove(key);}}/// <summary>/// Clear all cache data/// </summary>public virtual void Clear(){foreach (var item in Cache)Remove(item.Key);}} }

摘自:NopCommerce框架(http://nopcommerce.codeplex.com/)?

轉載于:https://www.cnblogs.com/morang/p/5371651.html

總結

以上是生活随笔為你收集整理的个人项目框架搭建 -- 缓存接口与实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。