XXProvider.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using Bowin.Common.JSON;
  7. using System.Web.Script.Serialization;
  8. using System.Configuration;
  9. namespace Bowin.Common.Cache
  10. {
  11. public class XXProvider : ICacheProvider
  12. {
  13. string XXRoot = ConfigurationManager.AppSettings["xixicache"] + "/";
  14. private string CacheKey_Format = ConfigurationManager.AppSettings["cacheKey"] + "_{0}";
  15. const int expire = 36000;
  16. private bool TryGet(string key, out object value)
  17. {
  18. value = null;
  19. try
  20. {
  21. Uri getUri = new Uri(XXRoot + "manager/get?k=" + key);
  22. WebRequest wq = WebRequest.Create(getUri);
  23. var r = wq.GetResponse();
  24. var sr = r.GetResponseStream();
  25. byte[] buffer = new byte[r.ContentLength];
  26. sr.Read(buffer, 0, buffer.Length);
  27. //JavaScriptSerializer j = new JavaScriptSerializer();
  28. //value = j.Deserialize(Encoding.Default.GetString(buffer), value.GetType());
  29. //value = Encoding.Default.GetString(buffer);
  30. value = Encoding.UTF8.GetString(buffer);
  31. return true;
  32. }
  33. catch (Exception ex)
  34. {
  35. return false;
  36. }
  37. }
  38. private bool Store(string key, object value)
  39. {
  40. try
  41. {
  42. Uri setUri = new Uri(XXRoot + "manager/set?k=" + key + "&v=" + value.ToJson() + "&e=" + expire.ToString());
  43. WebRequest wq = WebRequest.Create(setUri);
  44. var r = wq.GetResponse();
  45. return true;
  46. }
  47. catch (Exception ex)
  48. {
  49. return false;
  50. }
  51. }
  52. private bool Delete(string key)
  53. {
  54. try
  55. {
  56. Uri setUri = new Uri(XXRoot + "manager/del?k=" + key);
  57. WebRequest wq = WebRequest.Create(setUri);
  58. var r = wq.GetResponse();
  59. return true;
  60. }
  61. catch (Exception ex)
  62. {
  63. return false;
  64. }
  65. }
  66. public object Add(string key, object entry, DateTime utcExpiry)
  67. {
  68. object oOutObject = entry;
  69. key = string.Format(CacheKey_Format, key);
  70. if (!this.TryGet(key, out oOutObject))
  71. {
  72. this.Store(key, entry);
  73. }
  74. return oOutObject;
  75. }
  76. public object Get(string key)
  77. {
  78. object oOutObject = null;
  79. key = string.Format(CacheKey_Format, key);
  80. this.TryGet(key, out oOutObject);
  81. return oOutObject;
  82. }
  83. public void Remove(string key)
  84. {
  85. key = string.Format(CacheKey_Format, key);
  86. this.Delete(key);
  87. }
  88. public void Set(string key, object entry, DateTime utcExpiry)
  89. {
  90. object oOutObject = entry;
  91. key = string.Format(CacheKey_Format, key);
  92. if (this.TryGet(key, out oOutObject))
  93. {
  94. this.Store(key, entry);
  95. }
  96. else
  97. {
  98. this.Store(key, entry);
  99. }
  100. }
  101. }
  102. }