没啥说的,直接上代码

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. Linux基础学习笔记2-文件管理和重定向

    本节内容 1)文件系统结构元素 2)创建和查看文件 3)复制.转移和删除文件 4)软和硬链接 5)三种I/O设备 6)把I/O重定向至文件 7)使用管道 文件系统和结构 文件系统 文件和目录被组织成一 ...

  2. python爬虫scrapy之登录知乎

    下面我们看看用scrapy模拟登录的基本写法: 注意:我们经常调试代码的时候基本都用chrome浏览器,但是我就因为用了谷歌浏览器(它总是登录的时候不提示我用验证码,误导我以为登录时不需要验证码,其实 ...

  3. python爬虫之git的使用

    一.简单认识: 1.初始化文件夹为版本控制文件夹,首先建立一个文件夹,进入这个文件夹以后输入git init初始化这个文件夹. 2.Git几种位置概念 1.本地代码:本地更改完代码以后,虽然是存放在g ...

  4. PHP金额工具类之将阿利伯数字转换为大写中文数字

    1.将阿拉伯数字转换为中文大写数字 <?php namespace core\components; class PriceHelper extends \yii\base\Component{ ...

  5. ajax获得后台传来的一个json值,在js中获得其中的属性值

    首先 ajax的dataType需要设置为json, 默认的text获取属性值在jquery3.2.1中尝试不成功 获得属性值的方式: 类似数组,键值对的方式 下面例子: 设置dataType为jso ...

  6. 十分钟了结MySQL information_schema

    information_schema数据库是MySQL系统自带的数据库,它提供了数据库元数据的访问方式.感觉information_schema就像是MySQL实例的一个百科全书,记录了数据库当中大部 ...

  7. Nginx TSL/SSL优化握手性能

    L:131

  8. x = x &(x-1)

    判断一个数(x)的二进制含有几个1 int func(x) { ; while(x) { countx ++; x = x&(x-); } return countx; } 假定x = 999 ...

  9. 实现中英文混合string的逆向输出

    #include <iostream> using namespace std; // 输入一个字符串(包括英文和中文),将其反序输出, 如: // hello 今天真热 ---> ...

  10. 51Nod 1381 硬币游戏

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6445369.html 1381 硬币游戏 基准时间限制:1 秒 空间限制:131072 KB 分值 ...