OrderProductDbconn.cs 2.6 KB

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