WeiXin组件Senpac(五)【缓存策略】

基接口。

public interface IBaseCacheStrategy
{
    ICacheLock BeginCacheLock(string resourceName, string key, int retryCount = 0, TimeSpan retryDelay = new TimeSpan());
}

ICacheLock是什么呢?围绕锁的,有没有获得锁成功?开始锁,释放锁。

public interface ICacheLock : IDisposable
{
    bool LockSuccessful{get;set;}
    bool Lock(string resourceName);
    bool Lock(string resourceName, int retryCount, TimeSpan retryDelay);
    void UnLock(string resourceName);
}

缓存策略的接口基类是泛型,因为key和value都不一定是什么类型。

public interface IBaseCacheStrategy<TKey, TValue> : IBaseCacheStrategy
{
    string GetFinalKey(string key, bool isFullKey = false);
    void InsertToCache(TKey key, TValue value);
    void RemoveFromCache(TKey key, bool isFullKey = false);
    TValue Get(TKey key, bool isFullKey = false);
    IDictionary<TKey, TValue> GetAll();
    bool CheckExisted(TKey key, bool isFullKey = false);
    long GetCount();
    void Update(TKey key, TValue value, bool isFullKey = false);
}

在微信开发中,很多有凭据会放在一个类似容器的数据结构中,所有也需要针对容器的缓存策略。

public interface IContainerCacheStrategy : IBaseCacheStrategy<string, IBaseContainerBag>
{
    //针对容器的缓存策略也有获得所有,这里的所有就是一个字典集合,不过字典集合的类型已经明确了
    IDictionary<string, TBag> GetAll<TBag> where TBag : IBaseContainerBag;
    //针对容器的更新也非常明确了
    void UpdateContainerBag(string key, IBaseContaineerBag containerBag, bool isFullKey = false);
}

以上,有关容器的缓存策略,key一定是string类型,value的类型是IBaseContainerBag.

public interface IBaseContainerBag
{
    string Name{get;set;}
    string Key{get;set;}
    DateTime CacheTime{get;set;}
}
public interface IBaseContainerBag_AppId
{
    string AppId{get;set;}
}

redis使用

  • 网址:http://redis.io/
  • https://github.com/MSOpenTech/redis/releases
  • https://github.com/redis/redis/releases
  • 确保在服务中运行
  • 执行redis-cli.exe
  • 存储set mykey myval
  • 获取get mykey
  • 客户端工具:redisdesktop.com

如何设置?
所有的配置都放在一个类里。

public class SenparcWeixinSetting
{
    public string Cache_Redis_Configuration{get;set;}
}

放到DI容器里。

services.Configure<SenparcWeixinSetting>(Configuration.GetSection("SenparcWeixinSetting"));

在请求管道中使用

public void Configute(IApplicationBuilder app, IHostingEnvironment env, IOptions<SenparcWeixinSetting> senparcWeixinSetting)
{
    Config.SefaultSenpacWeixinSetting = senparcWeixinSetting.Value;
}

有一个私有方法注册缓存。

private void RegisterWeixinCache()
{
    var senparcWeixinSetting = Config.DefaultSenparcWeixinSetting;
    var redisConfiguration = senparcWeixinSetting.Cache_Redis_Configuration;
    RedisManager.ConfigurationOption = redisConiguration;
    if(!string.IsNullOrEmpty(redisConfiguration) && redisConfiguration !="")
    {
        CacheStrategyFactry.RegisterObjectCacheStrategy(() => RedisObjectCacheStrategy.Instance);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值