You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DbOutputCache.cs 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Linq;
  2. using iiie.CacheControl.DataAccess;
  3. using iiie.CacheControl.DBO;
  4. namespace iiie.CacheControl.Business.OutputCache
  5. {
  6. public class DbOutputCache : IOutputCache
  7. {
  8. public string ConnectionString { get; set; }
  9. private CacheControlEntities GetDb()
  10. {
  11. if (ConnectionString == null)
  12. return new CacheControlEntities();
  13. return new CacheControlEntities(ConnectionString);
  14. }
  15. public override CacheDbo Get(string key)
  16. {
  17. using (var db = GetDb())
  18. {
  19. var item = db.T_Cache.FirstOrDefault(x => x.cachekey == key);
  20. if (item == null)
  21. return null;
  22. return new CacheDbo
  23. {
  24. Date = item.date,
  25. ETag = item.etag,
  26. Content = item.content,
  27. ContentType = item.content_type
  28. };
  29. }
  30. }
  31. public override void Remove(string key)
  32. {
  33. using (var db = GetDb())
  34. {
  35. var item = db.T_Cache.FirstOrDefault(x => x.cachekey == key);
  36. if (item != null)
  37. {
  38. db.T_Cache.Remove(item);
  39. db.SaveChanges();
  40. }
  41. }
  42. }
  43. public override bool Contains(string key)
  44. {
  45. using (var db = GetDb())
  46. {
  47. return db.T_Cache.Any(x => x.cachekey == key);
  48. }
  49. }
  50. public override void Add(string key, CacheDbo o)
  51. {
  52. using (var db = GetDb())
  53. {
  54. db.T_Cache.Add(new T_Cache
  55. {
  56. cachekey = key,
  57. etag = o.ETag,
  58. content = o.Content,
  59. date = o.Date,
  60. content_type = o.ContentType
  61. });
  62. db.SaveChanges();
  63. }
  64. }
  65. }
  66. }