没啥说的,直接上代码

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. iframe全屏显示

    <iframe webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=" ...

  2. CBV源码分析+APIVIew源码分析

    {drf,resful,apiview,序列化组件,视图组件,认证组件,权限组件,频率组件,解析器,分页器,响应器,URL控制器,版本控制} 一.CBV源码分析准备工作: 新建一个Django项目 写 ...

  3. springmvc中model可以封装的数据类型

    查看源码可以知道,model中可以存放的数据类型 Model addAttribute(String var1, @Nullable Object var2); Model addAttribute( ...

  4. python爬虫scrapy之scrapy终端(Scrapy shell)

    Scrapy终端是一个交互终端,供您在未启动spider的情况下尝试及调试您的爬取代码. 其本意是用来测试提取数据的代码,不过您可以将其作为正常的Python终端,在上面测试任何的Python代码. ...

  5. Unable to handle kernel paging request at virtual address

    1.Unable to handle kernel paging request at virtual address 00000000 =====>越出内核地址空间范围,原因是由于使用空NUL ...

  6. 多IP地址--笔记

    多IP 地址特性使虚拟用户可以在一个load generator上运行且被识别为多个IP地址 1 虚拟IP是同一个generator上的多个IP,这种分配过程由controller自动来进行 2 对于 ...

  7. delphi中adoquery控件中某个字段Onvalidate事件的用法?

    procedure TForm2.ADOQuery1TestFieldValidate(Sender: TField);begin// Sender就是当前字段,可以在这里对Sender字段进行各种操 ...

  8. flask保存 文件到本地

    本篇队长介绍一下如何 把前端上传的文件保存 到 后端flask项目目录 首先讲一下上传.保存文件的思路: 第一步:前端通过post请求方式提交上传的文件 <input id="file ...

  9. How to remove unwant Explorer Context Menu

    HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell

  10. react用class关键字来创建组件

    创建组件之前,首先学习一个ES6的写法,叫做展开运算符. 比如我这里有两个数组.如何将第二个数组o2中的所有属性导入到数组o1中呢?一个个输太麻烦,所以就用到了展开运算符. var o2={ age: ...