LocalContainerCacheStrategy.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:LocalContainerCacheStrategy.cs
  17. 文件功能描述:本地容器缓存。
  18. 创建标识:Senparc - 20160308
  19. 修改标识:Senparc - 20160812
  20. 修改描述:v4.7.4 解决Container无法注册的问题
  21. 修改标识:Senparc - 20161024
  22. 修改描述:v4.8.3 废除LocalCacheHelper类,将LocalObjectCacheStrategy做为基类
  23. ----------------------------------------------------------------*/
  24. using System.Collections.Generic;
  25. using Senparc.Weixin.Containers;
  26. using Senparc.Weixin.Cache;
  27. using Senparc.CO2NET.Cache;
  28. using System;
  29. namespace Senparc.Weixin.Cache
  30. {
  31. ///// <summary>
  32. ///// 全局静态数据源帮助类
  33. ///// </summary>
  34. //public static class LocalContainerCacheHelper
  35. //{
  36. // /// <summary>
  37. // /// 所有数据集合的列表
  38. // /// </summary>
  39. // internal static IDictionary<string, IBaseContainerBag> LocalContainerCache { get; set; }
  40. // static LocalContainerCacheHelper()
  41. // {
  42. // LocalContainerBaseCacheStrategy() = new Dictionary<string, IBaseContainerBag>(StringComparer.OrdinalIgnoreCase);
  43. // }
  44. //}
  45. /// <summary>
  46. /// 本地容器缓存策略
  47. /// </summary>
  48. public sealed class LocalContainerCacheStrategy : BaseContainerCacheStrategy
  49. {
  50. #region IDomainExtensionCacheStrategy 成员
  51. public override ICacheStrategyDomain CacheStrategyDomain { get { return ContainerCacheStrategyDomain.Instance; } }
  52. #endregion
  53. #region 单例
  54. /// <summary>
  55. /// LocalCacheStrategy的构造函数
  56. /// </summary>
  57. private LocalContainerCacheStrategy() /*: base()*/
  58. {
  59. //使用底层缓存策略
  60. BaseCacheStrategy = () => LocalObjectCacheStrategy.Instance;
  61. //向底层缓存注册当前缓存策略
  62. base.RegisterCacheStrategyDomain(this);
  63. }
  64. //静态LocalCacheStrategy
  65. public static IContainerCacheStrategy Instance
  66. {
  67. get
  68. {
  69. return Nested.instance;//返回Nested类中的静态成员instance
  70. }
  71. }
  72. class Nested
  73. {
  74. static Nested()
  75. {
  76. }
  77. //将instance设为一个初始化的LocalCacheStrategy新实例
  78. internal static readonly LocalContainerCacheStrategy instance = new LocalContainerCacheStrategy();
  79. }
  80. #endregion
  81. #region BaseContainerCacheStrategy 成员
  82. public override IDictionary<string, TBag> GetAll<TBag>()
  83. {
  84. var dic = new Dictionary<string, TBag>();
  85. var baseCacheStrategy = BaseCacheStrategy();
  86. var cacheList = baseCacheStrategy.GetAll();
  87. foreach (var baseContainerBag in cacheList)
  88. {
  89. if (baseContainerBag.Value is TBag)
  90. {
  91. dic[baseContainerBag.Key] = (TBag)baseContainerBag.Value;
  92. }
  93. }
  94. return dic;
  95. }
  96. #endregion
  97. //public IDictionary<string, IBaseContainerBag> GetAll()
  98. //{
  99. // var dic = new Dictionary<string, IBaseContainerBag>();
  100. // foreach (var item in BaseCacheStrategy().GetAll())
  101. // {
  102. // if (item.Value is IBaseContainerBag)
  103. // {
  104. // dic[item.Key] = (IBaseContainerBag)item.Value;
  105. // }
  106. // }
  107. // return dic;
  108. //}
  109. //public ICacheLock BeginCacheLock(string resourceName, string key, int retryCount = 0, TimeSpan retryDelay = default(TimeSpan))
  110. //{
  111. // return new LocalCacheLock(BaseCacheStrategy() as LocalObjectCacheStrategy, resourceName, key, retryCount, retryDelay);
  112. // //return BaseCacheStrategy().BeginCacheLock(resourceName, key, retryCount, retryDelay);
  113. //}
  114. }
  115. }