123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using DirectService.Tools;
- using JLHHJSvr.BLL;
- using JLHHJSvr.Com.Model;
- using LJLib;
- using LJLib.DAL.SQL;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- namespace JLHHJSvr.Helper
- {
- internal sealed class CacheHelper : HelperBase
- {
- #region 基础资料Mapping
- /// <summary>
- /// 弹簧资料
- /// </summary>
- public class SpringMapping : ICacheMapping<int, u_spring>
- {
- public u_spring GetInstance(SqlCommand cmd, int springid)
- {
- var instance = new u_spring() { springid = springid };
- DbSqlHelper.SelectOne(cmd, instance, "line_diameter,gram_weight,height,center_diameter,caliber,cyclenum,roll_width,roll_length,arrangement_width,arrangement_height,springtypeid");
- return instance;
- }
- }
- /// <summary>
- /// 部门资料
- /// </summary>
- public class DeptMapping : ICacheMapping<int, u_dept>
- {
- public u_dept GetInstance(SqlCommand cmd, int deptid)
- {
- var instance = new u_dept() { deptid = deptid };
- DbSqlHelper.SelectOne(cmd, instance, "pricelistid,profitrate,moneyrate,discount,taxes_rate,managerate,com_profitrate,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate");
- return instance;
- }
- }
- #endregion
- #region 获取数据
- private static readonly Dictionary<Type, object> _cacheServiceRegistry = new Dictionary<Type, object>();
- public TEntity GetData<TKey, TEntity, TMapping>(TKey key) where TEntity: class,new() where TMapping : ICacheMapping<TKey, TEntity>, new()
- {
- var entityType = typeof(TEntity);
- if (!_cacheServiceRegistry.TryGetValue(entityType, out var serviceObj))
- {
- var cacheService = new CacheService<TKey, TEntity>(new GenericLoader<TKey, TEntity>(new TMapping()));
- _cacheServiceRegistry.Add(entityType, cacheService);
- serviceObj = cacheService;
- }
- var service = (CacheService<TKey, TEntity>)serviceObj;
- return service.Get(cmd,key);
- }
- #endregion
- }
- #region 核心
- public interface ICacheLoader<TKey, TValue> where TValue : class, new()
- {
- TValue Load(SqlCommand cmd, TKey key);
- }
- public interface ICacheMapping<TKey, TValue> where TValue : class, new()
- {
- TValue GetInstance(SqlCommand cmd, TKey key);
- }
- public class CacheService<TKey, TValue> where TValue : class, new()
- {
- private readonly LJCache<TKey, TValue> _cache;
- private readonly ICacheLoader<TKey, TValue> _loader;
- public CacheService(ICacheLoader<TKey, TValue> loader, int cacheMinutes)
- {
- _loader = loader;
- _cache = new LJCache<TKey, TValue> { DefaultAddMinutes = cacheMinutes };
- }
- public CacheService(ICacheLoader<TKey, TValue> loader)
- {
- _loader = loader;
- _cache = new LJCache<TKey, TValue> { DefaultAddMinutes = 120 };
- }
- public TValue Get(SqlCommand cmd,TKey key)
- {
- if (!_cache.TryGetValue(key, out TValue value))
- {
- value = _loader.Load(cmd, key);
- _cache.Add(key, value);
- }
- return ObjectHelper.DeepCopy(value);
- }
- }
- public class GenericLoader<TKey, TValue> : ICacheLoader<TKey, TValue> where TValue : class, new()
- {
- private readonly ICacheMapping<TKey, TValue> _mapping;
- public GenericLoader(ICacheMapping<TKey, TValue> mapping)
- {
- _mapping = mapping;
- }
- public TValue Load(SqlCommand cmd, TKey id)
- {
- return _mapping.GetInstance(cmd, id);
- }
- }
- #endregion
- }
|