LanguageCache.cs
上传用户:lanchensha
上传日期:2022-02-27
资源大小:7530k
文件大小:8k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. //------------------------------------------------------------------------------
  2. //                                  版权声明
  3. //DotNetTextBox免费版源码版权由小宝.NET及Aspxcn中华网工作室所有!
  4. //非盈利性个人网站可免费使用本控件,商业及盈利性网站请购买功能更强大的商业版本授
  5. //权或开发版,如发现任何个人或机构违反本声明,本站将对其追究法律责任!
  6. //商业授权及开发版购买地址:http://www.aspxcn.com.cn/dotnettextbox/default.htm
  7. //联系email:webmaster@aspxcn.com.cn
  8. //------------------------------------------------------------------------------
  9. using System;
  10. using System.Collections;
  11. using System.Text.RegularExpressions;
  12. using System.Web;
  13. using System.Web.Caching;
  14. namespace DotNetTextBox
  15. {
  16.     #region 自定义控件国际化功能(多语言)的缓存管理
  17.     /// <summary>
  18. /// 缓存管理
  19. /// </summary>
  20. public class LanguageCache
  21. {
  22. private LanguageCache(){}
  23.         //>> Based on Factor = 5 default value
  24.         public static readonly int DayFactor = 17280;
  25.         public static readonly int HourFactor = 720;
  26.         public static readonly int MinuteFactor = 12;
  27. public static readonly double SecondFactor = 0.2;
  28.         private static readonly Cache _cache;
  29.         private static int Factor = 5;
  30. public static void ReSetFactor(int cacheFactor)
  31. {
  32. Factor = cacheFactor;
  33. }
  34.         /// <summary>
  35.         /// Static initializer should ensure we only have to look up the current cache
  36.         /// instance once.
  37.         /// </summary>
  38.         static LanguageCache()
  39.         {
  40.             HttpContext context = HttpContext.Current;
  41.             if(context != null)
  42.             {
  43.                 _cache = context.Cache;
  44.             }
  45.             else
  46.             {
  47.                 _cache = HttpRuntime.Cache;
  48.             }
  49.         }
  50.         /// <summary>
  51.         /// 清空Cash对象
  52.         /// </summary>
  53.         public static void Clear()
  54.         {
  55.             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
  56.             ArrayList al = new ArrayList();
  57.             while(CacheEnum.MoveNext())
  58.             {
  59.                 al.Add(CacheEnum.Key);
  60.             }
  61.             foreach(string key in al)
  62.             {
  63.                 _cache.Remove(key);
  64.             }
  65.         }
  66.         /// <summary>
  67.         /// 根据正则表达式的模式移除Cache
  68.         /// </summary>
  69.         /// <param name="pattern">模式</param>
  70.         public static void RemoveByPattern(string pattern)
  71.         {
  72.             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
  73.             Regex regex = new Regex(pattern,RegexOptions.IgnoreCase|RegexOptions.Singleline|RegexOptions.Compiled);
  74.             while(CacheEnum.MoveNext())
  75.             {
  76.                 if(regex.IsMatch(CacheEnum.Key.ToString()))
  77.                     _cache.Remove(CacheEnum.Key.ToString());
  78.             }
  79.         }
  80.         /// <summary>
  81.         /// 根据键值移除Cache
  82.         /// </summary>
  83.         /// <param name="key"></param>
  84.         public static void Remove(string key)
  85.         {
  86.             _cache.Remove(key);
  87.         }
  88.         /// <summary>
  89.         /// 把对象加载到Cache
  90.         /// </summary>
  91.         /// <param name="key">键</param>
  92.         /// <param name="obj">对象</param>
  93.         public static void Insert(string key, object obj)
  94.         {
  95.             Insert(key,obj,null,1);
  96.         }
  97.         /// <summary>
  98.         /// 把对象加载到Cache,附加缓存依赖信息
  99.         /// </summary>
  100.         /// <param name="key"></param>
  101.         /// <param name="obj"></param>
  102.         /// <param name="dep"></param>
  103.         public static void Insert(string key, object obj, CacheDependency dep)
  104.         {
  105.             Insert(key,obj,dep,MinuteFactor * 3);
  106.         }
  107.         /// <summary>
  108.         /// 把对象加载到Cache,附加过期时间信息
  109.         /// </summary>
  110.         /// <param name="key"></param>
  111.         /// <param name="obj"></param>
  112.         /// <param name="seconds"></param>
  113.         public static void Insert(string key, object obj, int seconds)
  114.         {
  115.             Insert(key,obj,null,seconds);
  116.         }
  117.         /// <summary>
  118.         /// 把对象加载到Cache,附加过期时间信息和优先级
  119.         /// </summary>
  120.         /// <param name="key"></param>
  121.         /// <param name="obj"></param>
  122.         /// <param name="seconds"></param>
  123.         /// <param name="priority"></param>
  124.         public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
  125.         {
  126.             Insert(key,obj,null,seconds,priority);
  127.         }
  128.         /// <summary>
  129.         /// 把对象加载到Cache,附加缓存依赖和过期时间(多少秒后过期)
  130.         /// (默认优先级为Normal)
  131.         /// </summary>
  132.         /// <param name="key"></param>
  133.         /// <param name="obj"></param>
  134.         /// <param name="dep"></param>
  135.         /// <param name="seconds"></param>
  136.         public static void Insert(string key, object obj, CacheDependency dep, int seconds)
  137.         {
  138.            Insert(key,obj,dep,seconds,CacheItemPriority.Normal);
  139.         }
  140.         /// <summary>
  141.         /// 把对象加载到Cache,附加缓存依赖和过期时间(多少秒后过期)及优先级
  142.         /// </summary>
  143.         /// <param name="key"></param>
  144.         /// <param name="obj"></param>
  145.         /// <param name="dep"></param>
  146.         /// <param name="seconds"></param>
  147.         /// <param name="priority"></param>
  148.         public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
  149.         {
  150.             if(obj != null)
  151.             {
  152.                 _cache.Insert(key,obj,dep,DateTime.Now.AddSeconds(Factor * seconds), TimeSpan.Zero,priority,null);
  153.             }
  154.         }
  155.         /// <summary>
  156.         /// 把对象加到缓存并忽略优先级
  157.         /// </summary>
  158.         /// <param name="key"></param>
  159.         /// <param name="obj"></param>
  160.         /// <param name="secondFactor"></param>
  161. public static void MicroInsert (string key, object obj, int secondFactor) 
  162. {
  163. if(obj != null) {
  164. _cache.Insert(key,obj,null,DateTime.Now.AddSeconds(Factor * secondFactor), TimeSpan.Zero);
  165. }
  166. }
  167.         /// <summary>
  168.         /// 把对象加到缓存,并把过期时间设为最大值
  169.         /// </summary>
  170.         /// <param name="key"></param>
  171.         /// <param name="obj"></param>
  172.         public static void Max(string key, object obj)
  173.         {
  174.             Max(key,obj,null);
  175.         }
  176.         /// <summary>
  177.         /// 把对象加到缓存,并把过期时间设为最大值,附加缓存依赖信息
  178.         /// </summary>
  179.         /// <param name="key"></param>
  180.         /// <param name="obj"></param>
  181.         /// <param name="dep"></param>
  182.         public static void Max(string key, object obj, CacheDependency dep)
  183.         {
  184.             if(obj != null)
  185.             {
  186.                 _cache.Insert(key,obj,dep,DateTime.MaxValue,TimeSpan.Zero,CacheItemPriority.AboveNormal,null);
  187.             }
  188.         }
  189. /// <summary>
  190. /// 插入持久性缓存
  191. /// </summary>
  192. /// <param name="key"></param>
  193. /// <param name="obj"></param>
  194. public static void Permanent(string key, object obj)
  195. {
  196. Permanent(key,obj,null);
  197. }
  198.         /// <summary>
  199.         /// 插入持久性缓存,附加缓存依赖
  200.         /// </summary>
  201.         /// <param name="key"></param>
  202.         /// <param name="obj"></param>
  203.         /// <param name="dep"></param>
  204. public static void Permanent(string key, object obj, CacheDependency dep)
  205. {
  206. if(obj != null)
  207. {
  208. _cache.Insert(key,obj,dep,DateTime.MaxValue,TimeSpan.Zero,CacheItemPriority.NotRemovable,null);
  209. }
  210. }
  211.         /// <summary>
  212.         /// 根据键获取被缓存的对象
  213.         /// </summary>
  214.         /// <param name="key"></param>
  215.         /// <returns></returns>
  216.         public static object Get(string key)
  217.         {
  218.             return _cache[key];
  219.         }
  220.     
  221. /// <summary>
  222. /// Return int of seconds * SecondFactor
  223. /// </summary>
  224. public static int SecondFactorCalculate(int seconds)
  225. {
  226. // Insert method below takes integer seconds, so we have to round any fractional values
  227. return Convert.ToInt32(Math.Round((double)seconds * SecondFactor));
  228. }
  229.     }
  230.     #endregion
  231. }