BaseContainerCacheStrategy.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Senparc.CO2NET.Cache;
  2. using Senparc.Weixin.Containers;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Senparc.Weixin.Cache
  8. {
  9. public abstract class BaseContainerCacheStrategy : IContainerCacheStrategy
  10. {
  11. #region IDomainExtensionCacheStrategy 成员
  12. public abstract ICacheStrategyDomain CacheStrategyDomain { get; }
  13. /// <summary>
  14. /// 数据源缓存策略
  15. /// </summary>
  16. public Func<IBaseObjectCacheStrategy> BaseCacheStrategy { get; protected set; }
  17. #endregion
  18. #region IContainerCacheStrategy 成员
  19. public abstract IDictionary<string, TBag> GetAll<TBag>() where TBag : IBaseContainerBag;
  20. /// <summary>
  21. /// 获取单个ContainerBag
  22. /// </summary>
  23. /// <param name="key">缓存键</param>
  24. /// <param name="isFullKey">是否已经是完整的Key,如果不是,则会调用一次GetFinalKey()方法</param>
  25. /// <returns></returns>
  26. public virtual TBag GetContainerBag<TBag>(string key, bool isFullKey = false) where TBag : IBaseContainerBag
  27. {
  28. var baseCacheStrategy = BaseCacheStrategy();
  29. return baseCacheStrategy.Get<TBag>(key, isFullKey);
  30. }
  31. /// <summary>
  32. /// 向底层缓存注册当前缓存策略
  33. /// </summary>
  34. /// <param name="extensionCacheStrategy"></param>
  35. public void RegisterCacheStrategyDomain(IDomainExtensionCacheStrategy extensionCacheStrategy)
  36. {
  37. CacheStrategyDomainWarehouse.RegisterCacheStrategyDomain(extensionCacheStrategy);
  38. }
  39. public virtual void UpdateContainerBag(string key, IBaseContainerBag bag, TimeSpan? expiry = null, bool isFullKey = false)
  40. {
  41. var baseCacheStrategy = BaseCacheStrategy();
  42. baseCacheStrategy.Update(key, bag, expiry, isFullKey);
  43. }
  44. #endregion
  45. }
  46. }