没啥说的,直接上代码

1、缓存  CacheFactory 实现:

//-----------------------------------------------------------------------
// <copyright file="CacheFactory.cs" company="FenSiShengHuo, Ltd.">
// Copyright (c) 2018 , All rights reserved.
// </copyright>
//----------------------------------------------------------------------- using Senparc.CO2NET.Cache;
using Senparc.Weixin.Cache;
using System; namespace DotNet.WeChat.CommonService.Utilities
{
using DotNet.MVC.Infrastructure.Models;
/// <summary>
/// CacheFactory 基于
///
/// 修改记录
///
/// 2018-04-20 版本:1.0 JiShiYu 创建文件。
///
/// <author>
/// <name>JiShiYu</name>
/// <date>2018-04-20</date>
/// </author>
/// </summary>
public class CacheFactory
{
/// <summary>
/// 缓存处理
/// </summary>
/// <param name="cacheKey">缓存的Key</param>
/// <param name="proc">处理函数</param>
/// <param name="isCache">是否从缓存取</param>
/// <param name="refreshCache">是否强制刷新缓存</param>
/// <param name="expireAt">什么时候过期</param>
/// <returns></returns>
public static HashSetCacheModel<T> Cache<T>(string cacheKey, Func<HashSetCacheModel<T>> proc,bool isCache = false, bool refreshCache = false)
{
HashSetCacheModel<T> result;
if (!isCache)
{
result = proc();
}
else
{
IContainerCacheStrategy containerCache = LocalContainerCacheStrategy.Instance;
IBaseObjectCacheStrategy baseCache = containerCache.BaseCacheStrategy();
// 缓存
if (baseCache.CheckExisted(cacheKey)) //判断是否有缓存
{
//已经缓存
if (refreshCache)//是否强制刷新缓存
{
//强制刷新
result = proc();
baseCache.Update(cacheKey, result);
}
else
{
//不强制刷新
result = baseCache.Get<HashSetCacheModel<T>>(cacheKey);
// 判断是否过期了
if (result.ExpireAt < DateTime.Now)
{
result = proc();
baseCache.Update(cacheKey, result);
}
}
}
else
{
// 缓存中没有数据 获取一次存入缓存
result = proc();
baseCache.Set(cacheKey, result);
}
}
return result;
}
}
}
注意:Senparc 底层的Redis使用了HashSet数据结构

2、使用方法:注释里是以前的写法,使用CacheFactory后简化了操作
        /// <summary>
/// 获取微信用户
/// </summary>
/// <param name="weixinAppId"></param>
/// <param name="openId"></param>
/// <param name="refresh"></param>
/// <returns></returns>
protected WechatUserEntity GetWechatUser(string weixinAppId, string openId, bool refresh = false)
{
WechatUserEntity wechatUserEntity = null;
// 进行缓存
try
{
string cacheKey = "WechatUser:" + weixinAppId + ":" + openId;
HashSetCacheModel<WechatUserEntity> hashCacheModel= CacheFactory.Cache<WechatUserEntity>(cacheKey, () =>
{
WechatUserManager wechatUserManager = new WechatUserManager();
wechatUserEntity = wechatUserManager.GetObjectByWechatAppIdByOpenId(weixinAppId, openId);
var cacheModel = new HashSetCacheModel<WechatUserEntity>();
cacheModel.CreateOn = DateTime.Now;
cacheModel.ExpireAt = DateTime.Now.AddMinutes();
cacheModel.Value = wechatUserEntity;
return cacheModel;
});
return hashCacheModel.Value; //IContainerCacheStrategy containerCache = LocalContainerCacheStrategy.Instance;
//IBaseObjectCacheStrategy baseCache = containerCache.BaseCacheStrategy();
//HashSetCacheModel<WechatUserEntity> cacheModel = null;
//// 强制刷新缓存
//if (refresh)
//{
// if (baseCache.CheckExisted(key))
// {
// baseCache.RemoveFromCache(key);
// }
//}
//if (baseCache.CheckExisted(key))
//{
// cacheModel = baseCache.Get<HashSetCacheModel<WechatUserEntity>>(key);
// if (cacheModel.ExpireAt < DateTime.Now)
// {
// // 过期了,要更新一次
// WechatUserManager wechatUserManager = new WechatUserManager();
// wechatUserEntity = wechatUserManager.GetObjectByWechatAppIdByOpenId(weixinAppId, openId);
// cacheModel = new HashSetCacheModel<WechatUserEntity>();
// cacheModel.CreateOn = DateTime.Now;
// cacheModel.ExpireAt = DateTime.Now.AddMinutes(10);
// cacheModel.Value = wechatUserEntity;
// baseCache.Update(key, cacheModel);
// }
// wechatUserEntity = cacheModel.Value;
//}
//else
//{
// WechatUserManager wechatUserManager = new WechatUserManager();
// wechatUserEntity = wechatUserManager.GetObjectByWechatAppIdByOpenId(weixinAppId, openId);
// cacheModel = new HashSetCacheModel<WechatUserEntity>();
// cacheModel.CreateOn = DateTime.Now;
// cacheModel.ExpireAt = DateTime.Now.AddMinutes(10);
// cacheModel.Value = wechatUserEntity;
// baseCache.Set(key, cacheModel);
//}
}
catch (Exception ex)
{
NLogHelper.Warn(ex, "GetCurrentAutoreplyInfo,weixinAppId=" + weixinAppId);
} return wechatUserEntity;
}

3、HashSetCacheModel 实体,对Hset值的一次封装

//-----------------------------------------------------------------------
// <copyright file="WeChatMPController" company="FenSiShengHuo, Ltd.">
// Copyright (c) 2018 , All rights reserved.
// </copyright>
//----------------------------------------------------------------------- using System; namespace DotNet.MVC.Infrastructure.Models
{
/// <summary>
/// HashSetCacheModel<T>
/// HashSet结构
///
/// 修改纪录
///
/// 2018-07-02版本:1.0 JiShiYu 创建文件。
///
/// <author>
/// <name>JiShiYu</name>
/// <date>2018-07-02</date>
/// </author>
/// </summary>
public class HashSetCacheModel<T>
{
/// <summary>
/// 进入缓存的时间
/// </summary>
public DateTime CreateOn { set; get; } = DateTime.Now; /// <summary>
/// 缓存过期时间
/// </summary>
public DateTime ExpireAt { set; get; } /// <summary>
/// 缓存对象的值
/// </summary>
public T Value { get; set; }
}
}
												

基于Senparc.CO2NET 缓存策略扩展的缓存使用方法的更多相关文章

  1. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化   在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...

  2. 【安卓中的缓存策略系列】安卓缓存策略之磁盘缓存DiskLruCache

    安卓中的缓存包括两种情况即内存缓存与磁盘缓存,其中内存缓存主要是使用LruCache这个类,其中内存缓存我在[安卓中的缓存策略系列]安卓缓存策略之内存缓存LruCache中已经进行过详细讲解,如看官还 ...

  3. http缓存策略以及强缓存和协商缓存浅析

    http缓存策略以及强缓存和协商缓存浅析 本地缓存-强缓存 本地缓存,也就是我们常说的强缓存:是指当浏览器请求资源时,如果请求服务端的资源命中了浏览器本地的缓存资源,那么浏览器就不会发送真正请求给服务 ...

  4. Hibernate第十二篇【二级缓存介绍、缓存策略、查询缓存、集合缓存】

    Hibernate二级缓存介绍 前面我们已经讲解过了一级缓存,一级缓存也就是Session缓存,只在Session的范围内有效-作用时间就在Session的作用域中,范围比较小 Hibernate为我 ...

  5. web缓存策略之HTTP缓存大全

    一. web缓存总分类 数据库数据缓存 Web应用,特别是SNS类型的应用,往往关系比较复杂,数据库表繁多,如果频繁进行数据库查询,很容易导致数据库不堪重荷.为了提供查询的性能,会将查询后的数据放到内 ...

  6. http缓存策略-nginx只缓存js和css不缓存html

    使用缓存,是提升web性能最重要的方式之一. 在开发单页面应用的时候,大量的单静态资源引用都是依赖于html这个文件的.例如: <!doctype html> <html lang= ...

  7. 缓存策略:redis缓存之springCache

    最近通过同学,突然知道服务器的缓存有很多猫腻,这里通过网上查询其他人的资料,进行记录: 缓存策略 比较简单的缓存策略: 1.失效:应用程序先从cache取数据,没有得到,则从数据库中取数据,成功后,放 ...

  8. Web开发基本准则-55实录-缓存策略

    续上篇<Web开发基本准则-55实录-Web访问安全>. Web开发基本准则-55实录-缓存策略 郑昀 创建于2013年2月 郑昀 最后更新于2013年10月26日 提纲: Web访问安全 ...

  9. RDD概念、特性、缓存策略与容错

    一.RDD概念与特性 1. RDD的概念 RDD(Resilient Distributed Dataset),是指弹性分布式数据集.数据集:Spark中的编程是基于RDD的,将原始数据加载到内存变成 ...

随机推荐

  1. Chrome---谷歌浏览器修改用户缓存文件夹 如何设置缓存路径

    1.首先我们在电脑上打开chrome浏览器,然后地址栏输入chrome://Version,然后按下回车键,找到个人资料路径一项. 2.接下来我们选中个人资料路径后面所有的信息,右键点击信息后选择“复 ...

  2. js中this指向、箭头函数

    普通函数:this指向分为4种情况,1. obj.getName();//指向obj2.getName();//非严格模式下,指向window,严格模式下为undefined3. var a = ne ...

  3. Linux基础学习笔记3-用户权限

    本章内容 用户user 令牌token,identity Linux用户:Uername/UID 管理员:root,0 普通用户:1-65535 系统用户:1-499,1-999(Centos7) 对 ...

  4. java获取本机ip(排除虚拟机等一些ip)最终解,总算找到方法了

    本文参考https://blog.csdn.net/u011809209/article/details/77236602 本文参考https://blog.csdn.net/yinshuomail/ ...

  5. 部署ingress及使用

    一.下载yaml文件 wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.y ...

  6. Python:matplotlib绘制直方图

    使用hist方法来绘制直方图:     绘制直方图,最主要的是一个数据集data和需要划分的区间数量bins,另外你也可以设置一些颜色.类型参数: plt.hist(np.random.randn(1 ...

  7. HTML5 History API & URL 重定向

    HTML5 History API & URL 重定向 disabled server url redirect https://developer.mozilla.org/en-US/doc ...

  8. Lodop打印html数字间隔不一致

    在font-size属性控制数字大小的时候,可能会出现数字间隔有问题,间隔不一致,可尝试用其他字体大小试试,一般字体越小,越可能出现问题. 如图,前两个打印项都是form1,样式一个是style1,一 ...

  9. Linux压缩和解压命令

    zip命令: 压缩 :zip -r files.zip fileFolder 解压:unzip files.zip tar命令: 压缩:tar -cvf files.tar fileFolder 解压 ...

  10. winFormToMysql&&几个控件的数据绑定

    运行图: 代码: private void button1_Click(object sender, EventArgs e) { string str = "database=csharp ...