CacheHelper.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Web;
  3. using System.Collections;
  4. namespace Ant.Service.Utilities
  5. {
  6. public class CacheHelper
  7. {
  8. /// <summary>
  9. /// 获取数据缓存
  10. /// </summary>
  11. /// <param name="CacheKey">键</param>
  12. public static object GetCache(string CacheKey)
  13. {
  14. System.Web.Caching.Cache objCache = HttpRuntime.Cache;
  15. return objCache[CacheKey];
  16. }
  17. /// <summary>
  18. /// 设置数据缓存
  19. /// </summary>
  20. public static void SetCache(string CacheKey, object objObject)
  21. {
  22. System.Web.Caching.Cache objCache = HttpRuntime.Cache;
  23. objCache.Insert(CacheKey, objObject);
  24. }
  25. /// <summary>
  26. /// 设置数据缓存
  27. /// </summary>
  28. public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
  29. {
  30. System.Web.Caching.Cache objCache = HttpRuntime.Cache;
  31. objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
  32. }
  33. /// <summary>
  34. /// 设置数据缓存
  35. /// </summary>
  36. public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  37. {
  38. System.Web.Caching.Cache objCache = HttpRuntime.Cache;
  39. objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
  40. }
  41. /// <summary>
  42. /// 移除指定数据缓存
  43. /// </summary>
  44. public static void RemoveAllCache(string CacheKey)
  45. {
  46. System.Web.Caching.Cache _cache = HttpRuntime.Cache;
  47. _cache.Remove(CacheKey);
  48. }
  49. /// <summary>
  50. /// 移除全部缓存
  51. /// </summary>
  52. public static void RemoveAllCache()
  53. {
  54. System.Web.Caching.Cache _cache = HttpRuntime.Cache;
  55. IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
  56. while (CacheEnum.MoveNext())
  57. {
  58. _cache.Remove(CacheEnum.Key.ToString());
  59. }
  60. }
  61. }
  62. }