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