CacheUtils.cs 2.5 KB

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