CacheHelper.cs 2.4 KB

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