CacheContext.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // ***********************************************************************
  2. // Assembly : Helper
  3. // Author : yubaolee
  4. // Created : 12-16-2016
  5. //
  6. // Last Modified By : yubaolee
  7. // Last Modified On : 12-21-2016
  8. // 使用微软默认带超时的Cache
  9. // File: CacheContext.cs
  10. // ***********************************************************************
  11. using System;
  12. using System.Web;
  13. namespace Infrastructure.Cache
  14. {
  15. public class CacheContext : ICacheContext
  16. {
  17. private readonly System.Web.Caching.Cache _objCache = HttpRuntime.Cache;
  18. public override T Get<T>(string key)
  19. {
  20. System.Web.Caching.Cache objCache = HttpRuntime.Cache;
  21. return (T) objCache[key];
  22. }
  23. public override bool Set<T>(string key, T t, DateTime expire)
  24. {
  25. var obj = Get<T>(key);
  26. if (obj != null)
  27. {
  28. Remove(key);
  29. }
  30. _objCache.Insert(key, t, null, expire, System.Web.Caching.Cache.NoSlidingExpiration);
  31. return true;
  32. }
  33. public override bool Remove(string key)
  34. {
  35. _objCache.Remove(key);
  36. return true;
  37. }
  38. }
  39. }