没啥说的,直接上代码

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. Flutter的输入框TextField

    TextFiled组件的API 先来看一下TextFiled的构造方法: const TextField({ Key key, this.controller, this.focusNode, thi ...

  2. 解决方法:CentOS7用yum安装软件显示错误:cannot find a valid baseurl for repo: base/7/x86_64

    在Linux学习中,发现使用yum安装软件出现以下的错误: 百度了各种方法,很多人也发现光是修改REBOOT=yes也没用,多次进行挂载.修改网卡配置文件.重置IP地址.重启网络.创建又删除配置文件的 ...

  3. Python--文件、文件夹、压缩包、处理模块shutil

    高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中 1 import shutil 2 3 shuti ...

  4. countByValue

    [1,2,3,3]的RDD rdd.foreach(println)---------------------1 2 3 3

  5. java学习之—链表(4)

    /** * 使用链表实现队列 * Create by Administrator * 2018/6/19 0019 * 下午 4:37 **/ public class Link { public l ...

  6. Pyspark spark-submit 集群提交任务以及引入虚拟环境依赖包攻略

    网上提交 scala spark 任务的攻略非常多,官方文档其实也非常详细仔细的介绍了 spark-submit 的用法.但是对于 python 的提交提及得非常少,能查阅到的资料非常少导致是有非常多 ...

  7. epoch、 iteration和batchsize区别

    转自: https://blog.csdn.net/qq_27923041/article/details/74927398 深度学习中经常看到epoch. iteration和batchsize,下 ...

  8. HDU4341-Gold miner-分组DP

    模拟黄金矿工这个游戏,给出每一个金子的位置和所需时间,计算在给定时间内最大收益. 刚看这道题以为金子的位置没什么用,直接DP就行,WA了一发终于明白如果金子和人共线的话只能按顺序抓. 这就是需要考虑先 ...

  9. Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

    A. Technogoblet of Fire 题意:n个人分别属于m个不同的学校 每个学校的最强者能够选中 黑客要使 k个他选中的可以稳被选 所以就为这k个人伪造学校 问最小需要伪造多少个 思路:记 ...

  10. Codeforces551 C. GukiZ hates Boxes

    二分答案 + 贪心 传送门:$>here<$ $Solution$ 二分时间+贪心验证.思维难度主要在验证上,然而坑人的点却在n的取值上.那么先来谈如何验证.在已知时间的条件下,能否用一种 ...