Skip to content

Commit 60cb073

Browse files
committed
添加缓存方法
1 parent 07252b6 commit 60cb073

19 files changed

+1877
-4
lines changed

src/Senparc.TouTiao/Config.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static bool IsDebug
4949

5050
/// <summary>
5151
/// <para>头条全局配置</para>
52-
/// <para>注意:在程序运行过程中修改 SenparcWeixinSetting.Items 中的微信配置值,并不能修改 Container 中的对应信息(如AppSecret),</para>
52+
/// <para>注意:在程序运行过程中修改 SenparcWeixinSetting.Items 中的头条配置值,并不能修改 Container 中的对应信息(如AppSecret),</para>
5353
/// <para>如果需要修改头条信息(如AppSecret)应该使用 xxContainer.Register() 修改,这里的值也会随之更新。</para>
5454
/// </summary>
5555
public static SenparcToutiaoSetting SenparcToutiaoSetting { get; set; }
@@ -88,7 +88,7 @@ public static string DefaultCacheNamespace
8888
#region 小程序 API 的服务器地址(默认为:https://developer.toutiao.com)
8989

9090
/// <summary>
91-
/// 公众号(小程序)、开放平台 API 的服务器地址(默认为:https://api.weixin.qq.com)
91+
/// 小程序 API 的服务器地址(默认为:https://developer.toutiao.com)
9292
/// </summary>
9393
public static string ApiAppsHost { get; set; } = "https://developer.toutiao.com";
9494

src/Senparc.TouTiao/Senparc.TouTiao.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<PackageLicenseUrl>https://github.com/Senparc/TouTiaoSDK/blob/master/license.md</PackageLicenseUrl>
2020
<ProjectUrl>https://github.com/Senparc/TouTiaoSDK</ProjectUrl>
2121
<Title>Senparc.Toutiao.dll</Title>
22-
<Summary>微信公众号SDK for C#</Summary>
22+
<Summary>头条 SDK for C#</Summary>
2323
<PackageProjectUrl>https://github.com/Senparc/TouTiaoSDK</PackageProjectUrl>
2424
<PackageIconUrl>http://sdk.weixin.senparc.com/Images/Logo.jpg</PackageIconUrl>
2525
<RepositoryUrl>https://github.com/Senparc/TouTiaoSDK</RepositoryUrl>
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
文件名:BaseContainerCacheStrategy.cs
25+
文件功能描述:容器策略(领域缓存)基类。
26+
27+
28+
创建标识:Senparc - 20180614
29+
30+
修改标识:Senparc - 20200301
31+
修改描述:v6.7.303 BaseContainerCacheStrategy.UpdateContainerBag() 方法自动更新 CacheTime 值
32+
33+
----------------------------------------------------------------*/
34+
35+
36+
using Senparc.CO2NET.Cache;
37+
using Senparc.Toutiao.Containers;
38+
using System;
39+
using System.Collections.Generic;
40+
using System.Threading.Tasks;
41+
42+
namespace Senparc.Toutiao.Cache
43+
{
44+
/// <summary>
45+
/// 容器策略(领域缓存)基类
46+
/// </summary>
47+
public abstract class BaseContainerCacheStrategy : IContainerCacheStrategy
48+
{
49+
#region IDomainExtensionCacheStrategy 成员
50+
public abstract ICacheStrategyDomain CacheStrategyDomain { get; }
51+
52+
/// <summary>
53+
/// 数据源缓存策略
54+
/// </summary>
55+
public Func<IBaseObjectCacheStrategy> BaseCacheStrategy { get; protected set; }
56+
57+
#endregion
58+
59+
60+
#region IContainerCacheStrategy 成员
61+
62+
/// <summary>
63+
/// 向底层缓存注册当前缓存策略
64+
/// </summary>
65+
/// <param name="extensionCacheStrategy"></param>
66+
public void RegisterCacheStrategyDomain(IDomainExtensionCacheStrategy extensionCacheStrategy)
67+
{
68+
CacheStrategyDomainWarehouse.RegisterCacheStrategyDomain(extensionCacheStrategy);
69+
}
70+
71+
72+
#region 同步方法
73+
74+
/// <summary>
75+
/// 获取所有 Bag 对象
76+
/// </summary>
77+
/// <typeparam name="TBag"></typeparam>
78+
/// <returns></returns>
79+
public abstract IDictionary<string, TBag> GetAll<TBag>() where TBag : IBaseContainerBag;
80+
81+
82+
/// <summary>
83+
/// 获取单个ContainerBag
84+
/// </summary>
85+
/// <param name="key">缓存键</param>
86+
/// <param name="isFullKey">是否已经是完整的Key,如果不是,则会调用一次GetFinalKey()方法</param>
87+
/// <returns></returns>
88+
public virtual TBag GetContainerBag<TBag>(string key, bool isFullKey = false) where TBag : IBaseContainerBag
89+
{
90+
var baseCacheStrategy = BaseCacheStrategy();
91+
return baseCacheStrategy.Get<TBag>(key, isFullKey);
92+
}
93+
94+
95+
public virtual void UpdateContainerBag(string key, IBaseContainerBag bag, TimeSpan? expiry = null, bool isFullKey = false)
96+
{
97+
var baseCacheStrategy = BaseCacheStrategy();
98+
bag.CacheTime = SystemTime.Now;//更新缓存时间(上一级 Container 通常已经有设置,这里是为了小粒度确保一下)
99+
baseCacheStrategy.Update(key, bag, expiry, isFullKey);
100+
}
101+
102+
#endregion
103+
104+
#region 异步方法
105+
106+
107+
/// <summary>
108+
/// 【异步方法】获取所有 Bag 对象
109+
/// </summary>
110+
/// <typeparam name="TBag"></typeparam>
111+
/// <returns></returns>
112+
public abstract Task<IDictionary<string, TBag>> GetAllAsync<TBag>() where TBag : IBaseContainerBag;
113+
114+
115+
/// <summary>
116+
/// 【异步方法】获取单个ContainerBag
117+
/// </summary>
118+
/// <param name="key">缓存键</param>
119+
/// <param name="isFullKey">是否已经是完整的Key,如果不是,则会调用一次GetFinalKey()方法</param>
120+
/// <returns></returns>
121+
public virtual async Task<TBag> GetContainerBagAsync<TBag>(string key, bool isFullKey = false) where TBag : IBaseContainerBag
122+
{
123+
var baseCacheStrategy = BaseCacheStrategy();
124+
return await baseCacheStrategy.GetAsync<TBag>(key, isFullKey).ConfigureAwait(false);
125+
}
126+
127+
/// <summary>
128+
/// 【异步方法】更新 ContainerBag
129+
/// </summary>
130+
/// <param name="key"></param>
131+
/// <param name="bag"></param>
132+
/// <param name="expiry"></param>
133+
/// <param name="isFullKey"></param>
134+
public virtual async Task UpdateContainerBagAsync(string key, IBaseContainerBag bag, TimeSpan? expiry = null, bool isFullKey = false)
135+
{
136+
var baseCacheStrategy = BaseCacheStrategy();
137+
bag.CacheTime = SystemTime.Now;//更新缓存时间(上一级 Container 通常已经有设置,这里是为了小粒度确保一下)
138+
await baseCacheStrategy.UpdateAsync(key, bag, expiry, isFullKey).ConfigureAwait(false);
139+
}
140+
141+
#endregion
142+
143+
#endregion
144+
}
145+
}

0 commit comments

Comments
 (0)