没啥说的,直接上代码

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. embed标签的flash层级太高问题

    因为客户要求,项目得兼容IE的兼容模式 页面到了flash都会遮挡底部悬浮的导航. 改变浮动窗口和embed的层级还是不可以.应该不是层级的关系. 最后百度解决方案:在embed标签内添加了wmode ...

  2. Python 中关于 round 函数的小坑

    参考: http://www.runoob.com/w3cnote/python-round-func-note.html

  3. python爬虫之git的使用(github的使用)

    上面博文中我们简单的了解了一下基本的git操作,但是我们都是将代码放到了本地的仓库里面,但是如果我们是一个团队开发的话,肯定不会放到每个人的本地,必须得有个统一的地方存放代码,国外的大家都在使用git ...

  4. 【转】MySQL sql_mode 说明(及处理一起 sql_mode 引发的问题)

    1. MySQL 莫名变成了 Strict SQL Mode 最近测试组那边反应数据库部分写入失败,app层提示是插入成功,但表里面里面没有产生数据,而两个写入操作的另外一个表有数据.因为 inser ...

  5. DAY03、基本数据类型和运算符

    一.基本数据类型的使用 1.整型int: 作用:用来记录年龄.等级.数量 定义:age=18 使用:数学运算与比较运算: 例:print(10>3) print(10/3) 2.浮点型float ...

  6. codeforces259B

    Little Elephant and Magic Square CodeForces - 259B Little Elephant loves magic squares very much. A  ...

  7. no module named 'win32api'问题

    运行scrapy时,报错no module named 'win32api' 解决办法: https://github.com/mhammond/pywin32/releases 下载对于python ...

  8. yum安装时rpmdb损坏解决方法

    解决方案如下: # cd /var/lib/rpm # rm -f /var/lib/rpm/__db* # db_verify Packages # rpm --rebuilddb

  9. Github提交本地代码

     第一步:建立git仓库 cd到你的本地项目根目录下,执行git命令 git init 第二步:将项目的所有文件添加到仓库中 git add . 如果想添加某个特定的文件,只需把.换成特定的文件名即可 ...

  10. pysphere VMware控制模块的一些函数的说明

    对于虚拟机的操作获得虚拟机对象 当你正常连接了服务器后,你就可以使用以下两种方式来得到虚拟机对象. get_vm_by_path get_vm_by_name 虚拟机路径可以从虚拟机右键信息中的”Ed ...