ProductNormDbconn.cs 2.4 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 ProductNormDbconn
  8. {
  9. public readonly static ProductNormDbconn Instance = new ProductNormDbconn();
  10. #region 获取单个字段
  11. public ProductNorm Get(int Id)
  12. {
  13. string key = "ProductNorm:" + Id;
  14. if (RedisDbconn.Instance.Exists(key))
  15. {
  16. ProductNorm obj = RedisDbconn.Instance.Get<ProductNorm>(key);
  17. if (obj != null)
  18. {
  19. return obj;
  20. }
  21. }
  22. WebCMSEntities db = new WebCMSEntities();
  23. ProductNorm orderPro = db.ProductNorm.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<int> GetList(int ProductId = 1, int pageNum = 1, int pageSize = 10)
  34. {
  35. string key = "ProductNorms:" + ProductId;
  36. List<int> list = new List<int>();
  37. if (RedisDbconn.Instance.Exists(key))
  38. {
  39. list = RedisDbconn.Instance.GetList<int>(key, pageNum, pageSize);
  40. if (list.Count > 0)
  41. {
  42. return list;
  43. }
  44. }
  45. List<int> newlist = new List<int>();
  46. WebCMSEntities db = new WebCMSEntities();
  47. var mysqllist = db.ProductNorm.Select(m => new { m.Id, m.ProductId }).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.Id);
  53. }
  54. RedisDbconn.Instance.Clear(key);
  55. foreach (int 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. }