CacheHelper.cs 3.9 KB

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