GuidCache.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using SqlSugar;
  8. using EInk.Models;
  9. namespace DataAcquisition
  10. {
  11. public class GuidCache
  12. {
  13. public static Queue<string> GuidQueue = new Queue<string>();
  14. private static int capacity = 3000;
  15. private static IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
  16. private static ISqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
  17. {
  18. DbType = SqlSugar.DbType.PostgreSQL,
  19. ConnectionString = config.GetValue<string>("LocalDbConnectionString"),
  20. IsAutoCloseConnection = true
  21. });
  22. public static void GetGuidCache()
  23. {
  24. DateTime endtime = DateTime.Now;
  25. endtime = endtime.AddHours(-6);
  26. var QueryResult = db.Queryable<Lot2Lot3Model>().Where(x => x.create_time >= endtime).OrderBy(x => x.create_time).Select(it => it.lot_id).ToList();//返回guid字段
  27. if (QueryResult != null && QueryResult.Count != 0)
  28. {
  29. if (QueryResult.Count < capacity)
  30. {
  31. GuidQueue = new Queue<string>(QueryResult);
  32. }
  33. else
  34. {
  35. GuidQueue = new Queue<string>(QueryResult.GetRange(QueryResult.Count - capacity, capacity));
  36. }
  37. }
  38. }
  39. public static bool ContainVerification(string guid)
  40. {
  41. if (GuidQueue.Contains(guid))
  42. {
  43. return true;
  44. }
  45. else
  46. {
  47. return false;
  48. }
  49. }
  50. public static void Add(string guid)
  51. {
  52. if (GuidQueue.Count() >= capacity)
  53. {
  54. int i = GuidQueue.Count() - capacity + 1;
  55. for (; i > 0; i--)
  56. {
  57. GuidQueue.Dequeue();
  58. }
  59. }
  60. GuidQueue.Enqueue(guid);
  61. }
  62. }
  63. }