CacheHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. using System.Linq;
  11. using System.Reflection;
  12. namespace JLHHJSvr.Helper
  13. {
  14. internal sealed class CacheHelper : HelperBase
  15. {
  16. #region 基础资料Mapping
  17. /// <summary>
  18. /// 弹簧资料
  19. /// </summary>
  20. public class SpringMapping : ICacheMapping<int, u_spring>
  21. {
  22. public u_spring GetInstance(SqlCommand cmd, int springid)
  23. {
  24. var instance = new u_spring() { springid = springid };
  25. DbSqlHelper.SelectOne(cmd, instance, "line_diameter,gram_weight,height,center_diameter,caliber,cyclenum,roll_width,roll_length,arrangement_width,arrangement_height,springtypeid");
  26. return instance;
  27. }
  28. }
  29. /// <summary>
  30. /// 部门资料
  31. /// </summary>
  32. public class DeptMapping : ICacheMapping<int, u_dept>
  33. {
  34. public u_dept GetInstance(SqlCommand cmd, int deptid)
  35. {
  36. var instance = new u_dept() { deptid = deptid };
  37. DbSqlHelper.SelectOne(cmd, instance, "pricelistid,profitrate,moneyrate,discount,taxes_rate,managerate,com_profitrate,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate");
  38. return instance;
  39. }
  40. }
  41. #endregion
  42. #region 获取数据
  43. private static readonly Dictionary<Type, object> _cacheServiceRegistry = new Dictionary<Type, object>();
  44. public TEntity GetData<TEntity, TMapping>(object key)
  45. where TEntity : class, new()
  46. where TMapping : new()
  47. {
  48. // 找到接口中的 TKey
  49. var mappingType = typeof(TMapping);
  50. var iface = mappingType.GetInterfaces()
  51. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICacheMapping<,>));
  52. if (iface == null)
  53. throw new InvalidOperationException($"{mappingType.Name} must implement ICacheMapping<TKey, TValue>");
  54. var keyType = iface.GetGenericArguments()[0];
  55. // 获取私有泛型方法
  56. var method = typeof(CacheHelper).GetMethod(nameof(GetDataInternal), BindingFlags.Instance | BindingFlags.NonPublic);
  57. var gm = method.MakeGenericMethod(keyType, typeof(TEntity), mappingType);
  58. // 调用并返回(Invoke 返回 object)
  59. return (TEntity)gm.Invoke(this, new object[] { key });
  60. }
  61. private TValue GetDataInternal<TKey, TValue, TMapping>(object key)
  62. where TMapping : ICacheMapping<TKey, TValue>, new()
  63. where TValue : class, new()
  64. {
  65. var mappingType = typeof(TMapping);
  66. if (!_cacheServiceRegistry.TryGetValue(mappingType, out var serviceObj))
  67. {
  68. // 没有就创建
  69. var cacheService = new CacheService<TKey, TValue>(new GenericLoader<TKey, TValue>(new TMapping()));
  70. _cacheServiceRegistry.Add(mappingType, cacheService);
  71. serviceObj = cacheService;
  72. }
  73. var service = (ICacheService<TKey, TValue>)serviceObj;
  74. return service.Get(cmd, (TKey)key);
  75. }
  76. #endregion
  77. }
  78. }