MembaseProvider.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Membase;
  6. using Enyim.Caching.Memcached;
  7. using System.Configuration;
  8. namespace Bowin.Common.Cache
  9. {
  10. public class MembaseCacheProvider : ICacheProvider
  11. {
  12. public static readonly MembaseClient Client = new MembaseClient();
  13. private string CacheKey_Format = ConfigurationManager.AppSettings["cacheKey"] + "_{0}";
  14. public object Add(string key, object entry, DateTime utcExpiry)
  15. {
  16. object oOutObject = entry;
  17. key = string.Format(CacheKey_Format, key);
  18. if (!Client.TryGet(key, out oOutObject))
  19. {
  20. Client.Store(StoreMode.Add, key, entry, utcExpiry);
  21. }
  22. return oOutObject;
  23. }
  24. public object Get(string key)
  25. {
  26. object oOutObject = null;
  27. key = string.Format(CacheKey_Format, key);
  28. Client.TryGet(key, out oOutObject);
  29. return oOutObject;
  30. }
  31. public void Remove(string key)
  32. {
  33. key = string.Format(CacheKey_Format, key);
  34. Client.Remove(key);
  35. }
  36. public void Set(string key, object entry, DateTime utcExpiry)
  37. {
  38. object oOutObject = entry;
  39. key = string.Format(CacheKey_Format, key);
  40. if (Client.TryGet(key, out oOutObject))
  41. {
  42. Client.Store(StoreMode.Replace, key, entry, utcExpiry);
  43. }
  44. else
  45. {
  46. Client.Store(StoreMode.Add, key, entry, utcExpiry);
  47. }
  48. }
  49. }
  50. }