HttpCacheProvider.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Caching;
  6. using Bowin.Common.JSON;
  7. namespace Bowin.Common.Cache
  8. {
  9. public class HttpCacheProvider : ICacheProvider
  10. {
  11. private static System.Web.Caching.Cache cache
  12. {
  13. get
  14. {
  15. return System.Web.HttpContext.Current.Cache;
  16. }
  17. }
  18. static HttpCacheProvider()
  19. {
  20. }
  21. public object Add(string key, object entry, DateTime utcExpiry)
  22. {
  23. var result = Get(key);
  24. if (result == null)
  25. {
  26. cache.Insert(key, entry, null, System.Web.Caching.Cache.NoAbsoluteExpiration, utcExpiry.Subtract(DateTime.Now),
  27. CacheItemPriority.High, delegate { });
  28. result = entry;
  29. }
  30. return result;
  31. }
  32. public object Get(string key)
  33. {
  34. return cache[key];
  35. }
  36. public void Remove(string key)
  37. {
  38. cache.Remove(key);
  39. }
  40. public void Set(string key, object entry, DateTime utcExpiry)
  41. {
  42. var result = Get(key);
  43. if (result == null)
  44. {
  45. cache.Insert(key, entry, null, System.Web.Caching.Cache.NoAbsoluteExpiration, utcExpiry.Subtract(DateTime.Now),
  46. CacheItemPriority.High, delegate { });
  47. result = entry;
  48. }
  49. else
  50. {
  51. cache[key] = entry;
  52. }
  53. }
  54. }
  55. }