CacheHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using DirectService.Tools;
  2. using JLHHJSvr.BLL;
  3. using JLHHJSvr.Com.Model;
  4. using LJLib;
  5. using LJLib.DAL.SQL;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Data.SqlClient;
  10. namespace JLHHJSvr.Helper
  11. {
  12. internal sealed class CacheHelper : HelperBase
  13. {
  14. #region 基础资料Mapping
  15. /// <summary>
  16. /// 弹簧资料
  17. /// </summary>
  18. public class SpringMapping : ICacheMapping<int, u_spring>
  19. {
  20. public u_spring GetInstance(SqlCommand cmd, int springid)
  21. {
  22. var instance = new u_spring() { springid = springid };
  23. DbSqlHelper.SelectOne(cmd, instance, "line_diameter,gram_weight,height,center_diameter,caliber,cyclenum,roll_width,roll_length,arrangement_width,arrangement_height,springtypeid");
  24. return instance;
  25. }
  26. }
  27. /// <summary>
  28. /// 部门资料
  29. /// </summary>
  30. public class DeptMapping : ICacheMapping<int, u_dept>
  31. {
  32. public u_dept GetInstance(SqlCommand cmd, int deptid)
  33. {
  34. var instance = new u_dept() { deptid = deptid };
  35. DbSqlHelper.SelectOne(cmd, instance, "pricelistid,profitrate,moneyrate,discount,taxes_rate,managerate,com_profitrate,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate");
  36. return instance;
  37. }
  38. }
  39. #endregion
  40. #region 获取数据
  41. private static readonly Dictionary<Type, object> _cacheServiceRegistry = new Dictionary<Type, object>();
  42. public TEntity GetData<TKey, TEntity, TMapping>(TKey key) where TEntity: class,new() where TMapping : ICacheMapping<TKey, TEntity>, new()
  43. {
  44. var entityType = typeof(TEntity);
  45. if (!_cacheServiceRegistry.TryGetValue(entityType, out var serviceObj))
  46. {
  47. var cacheService = new CacheService<TKey, TEntity>(new GenericLoader<TKey, TEntity>(new TMapping()));
  48. _cacheServiceRegistry.Add(entityType, cacheService);
  49. serviceObj = cacheService;
  50. }
  51. var service = (CacheService<TKey, TEntity>)serviceObj;
  52. return service.Get(cmd,key);
  53. }
  54. #endregion
  55. }
  56. #region 核心
  57. public interface ICacheLoader<TKey, TValue> where TValue : class, new()
  58. {
  59. TValue Load(SqlCommand cmd, TKey key);
  60. }
  61. public interface ICacheMapping<TKey, TValue> where TValue : class, new()
  62. {
  63. TValue GetInstance(SqlCommand cmd, TKey key);
  64. }
  65. public class CacheService<TKey, TValue> where TValue : class, new()
  66. {
  67. private readonly LJCache<TKey, TValue> _cache;
  68. private readonly ICacheLoader<TKey, TValue> _loader;
  69. public CacheService(ICacheLoader<TKey, TValue> loader, int cacheMinutes)
  70. {
  71. _loader = loader;
  72. _cache = new LJCache<TKey, TValue> { DefaultAddMinutes = cacheMinutes };
  73. }
  74. public CacheService(ICacheLoader<TKey, TValue> loader)
  75. {
  76. _loader = loader;
  77. _cache = new LJCache<TKey, TValue> { DefaultAddMinutes = 120 };
  78. }
  79. public TValue Get(SqlCommand cmd,TKey key)
  80. {
  81. if (!_cache.TryGetValue(key, out TValue value))
  82. {
  83. value = _loader.Load(cmd, key);
  84. _cache.Add(key, value);
  85. }
  86. return ObjectHelper.DeepCopy(value);
  87. }
  88. }
  89. public class GenericLoader<TKey, TValue> : ICacheLoader<TKey, TValue> where TValue : class, new()
  90. {
  91. private readonly ICacheMapping<TKey, TValue> _mapping;
  92. public GenericLoader(ICacheMapping<TKey, TValue> mapping)
  93. {
  94. _mapping = mapping;
  95. }
  96. public TValue Load(SqlCommand cmd, TKey id)
  97. {
  98. return _mapping.GetInstance(cmd, id);
  99. }
  100. }
  101. #endregion
  102. }