RoadFlow2.1 临时演示

Cache.cs 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Web.Caching;
  7. namespace RoadFlow.Cache.InProc
  8. {
  9. /// <summary>
  10. /// .net自带缓存类
  11. /// </summary>
  12. public class Cache : Interface.ICache
  13. {
  14. public static object LockObject = new object();
  15. private System.Web.Caching.Cache cache = HttpContext.Current.Cache;
  16. /// <summary>
  17. /// 插入缓存
  18. /// </summary>
  19. /// <param name="key"></param>
  20. /// <param name="obj"></param>
  21. /// <returns></returns>
  22. public bool Insert(string key, object obj)
  23. {
  24. if (obj == null) return false;
  25. lock (LockObject)
  26. {
  27. cache.Insert(key, obj, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);
  28. }
  29. return true;
  30. }
  31. /// <summary>
  32. /// 插入缓存
  33. /// </summary>
  34. /// <param name="key"></param>
  35. /// <param name="obj"></param>
  36. /// <returns></returns>
  37. public bool Insert(string key, object obj, DateTime expiry)
  38. {
  39. if (obj == null) return false;
  40. lock (LockObject)
  41. {
  42. cache.Insert(key, obj, null, expiry, System.Web.Caching.Cache.NoSlidingExpiration,
  43. System.Web.Caching.CacheItemPriority.Normal, null);
  44. }
  45. return true;
  46. }
  47. /// <summary>
  48. /// 获取缓存
  49. /// </summary>
  50. /// <param name="key"></param>
  51. /// <returns></returns>
  52. public object Get(string key)
  53. {
  54. return cache.Get(key);
  55. }
  56. /// <summary>
  57. /// 移出缓存
  58. /// </summary>
  59. /// <param name="key"></param>
  60. public bool Remove(string key)
  61. {
  62. object lockObj = new object();
  63. lock (lockObj)
  64. {
  65. cache.Remove(key);
  66. }
  67. return true;
  68. }
  69. /// <summary>
  70. /// 移出所有缓存
  71. /// </summary>
  72. /// <returns></returns>
  73. public void RemoveAll()
  74. {
  75. for (int i = 0; i < cache.Count; i++)
  76. {
  77. }
  78. }
  79. }
  80. }