ProductNormItemDbconn.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.MainModels;
  5. namespace MySystem
  6. {
  7. public class ProductNormItemDbconn
  8. {
  9. public readonly static ProductNormItemDbconn Instance = new ProductNormItemDbconn();
  10. #region 获取单个字段
  11. public ProductNormItem Get(int Id)
  12. {
  13. string key = "ProductNormItem:" + Id;
  14. if (RedisDbconn.Instance.Exists(key))
  15. {
  16. ProductNormItem obj = RedisDbconn.Instance.Get<ProductNormItem>(key);
  17. if (obj != null)
  18. {
  19. return obj;
  20. }
  21. }
  22. WebCMSEntities db = new WebCMSEntities();
  23. ProductNormItem orderPro = db.ProductNormItem.FirstOrDefault(m => m.Id == Id);
  24. if (orderPro != null)
  25. {
  26. RedisDbconn.Instance.Set(key, orderPro);
  27. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
  28. }
  29. return orderPro;
  30. }
  31. #endregion
  32. #region 获取列表
  33. public List<ProductNormItem> GetList(int ProductId = 1, int pageNum = 1, int pageSize = 10)
  34. {
  35. string key = "ProductNormItem:" + ProductId;
  36. List<ProductNormItem> list = new List<ProductNormItem>();
  37. if (RedisDbconn.Instance.Exists(key))
  38. {
  39. list = RedisDbconn.Instance.GetList<ProductNormItem>(key, pageNum, pageSize);
  40. if (list.Count > 0)
  41. {
  42. return list;
  43. }
  44. }
  45. List<ProductNormItem> newlist = new List<ProductNormItem>();
  46. WebCMSEntities db = new WebCMSEntities();
  47. var mysqllist = db.ProductNormItem.Where(m=>m.ProductId == ProductId).OrderByDescending(m => m.Id).ToList();
  48. if (mysqllist.Count > 0)
  49. {
  50. foreach (var sub in mysqllist)
  51. {
  52. newlist.Add(sub);
  53. }
  54. RedisDbconn.Instance.Clear(key);
  55. foreach (ProductNormItem sub in newlist)
  56. {
  57. RedisDbconn.Instance.AddRightList(key, sub);
  58. }
  59. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
  60. }
  61. db.Dispose();
  62. return newlist;
  63. }
  64. #endregion
  65. }
  66. }