ProductsDbconn.cs 2.5 KB

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