CacheManager Net Core Redis 缓存
using CacheManager.Core;
using System;
using System.Collections.Generic;
using System.Text; namespace Service.Cache
{
public class QKCacheManager
{
public static QKCacheFactory factory = new QKCacheFactory();
/// <summary>
/// 读取缓存
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static T GetCache<T>(CacheKeyEnum cacheKey)
{
var cacheInfo = factory.Create(cacheKey);
var result = cacheInfo.Get();
if (result != null)
{
return (T)Convert.ChangeType(result, typeof(T));
}
else
{
return default(T);
}
} /// <summary>
/// 默认缓存时间
/// </summary>
static int ExpireSeconds = 30;
/// <summary>
/// 新增
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="expire"></param>
/// <returns></returns>
public static bool Add<T>(ICacheManager<T> cache, string key, T obj)
{
return Add(cache, key, obj, ExpireSeconds);
}
/// <summary>
/// 放入缓存,如果存在则替换,否则新建
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cache"></param>
/// <param name="key">缓存Key</param>
/// <param name="obj">缓存对象</param>
/// <param name="expire">缓存时限(秒)</param>
/// <returns>True:放入缓存成功,False:不做缓存或放入缓存失败</returns>
public static bool Add<T>(ICacheManager<T> cache, string key, T obj, int expire = 0)
{
bool result = false;
expire = expire > 0 ? expire : ExpireSeconds;
if (obj != null)
{
cache.Put(new CacheItem<T>(key, obj,
ExpirationMode.Absolute,
TimeSpan.FromSeconds(expire)));
result = true;
}
return result;
}
/// <summary>
/// 移除指定缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cache"></param>
/// <param name="key"></param>
/// <returns></returns>
public static bool Remove<T>(ICacheManager<T> cache, string key)
{
bool result = false;
if (string.IsNullOrEmpty(key))
{
cache.Remove(key);
result = true;
}
return result;
}
/// <summary>
/// 获取指定缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cache"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(ICacheManager<T> cache, string key)
{
return cache.Get<T>(key);
} /// <summary>
/// 生成缓存key
/// </summary>
/// <param name="dtoName">当前缓存的dto</param>
/// <returns></returns>
public static string GetCacheKey(string dtoName)
{
return GetCacheKey(dtoName, 0);
}
/// <summary>
/// 生成缓存key
/// </summary>
/// <param name="dtoName">当前缓存的dto</param>
/// <param name="pageIndex">缓存当前页索引</param>
/// <returns></returns>
public static string GetCacheKey(string dtoName, int pageIndex)
{
return GetCacheKey("DataCenter", dtoName, pageIndex);
}
/// <summary>
/// 生成缓存key
/// </summary>
/// <param name="regionName">区域名称</param>
/// <param name="dtoName">当前缓存的dto</param>
/// <returns></returns>
public static string GetCacheKey(string regionName, string dtoName)
{
return GetCacheKey(regionName, dtoName, 0);
}
/// <summary>
/// 生成缓存key
/// </summary>
/// <param name="regionName">区域名称</param>
/// <param name="dtoName">当前缓存的dto</param>
/// <param name="pageIndex">缓存当前页索引</param>
/// <returns></returns>
public static string GetCacheKey(string regionName, string dtoName, int pageIndex)
{
return regionName + "_" + dtoName + "_" + pageIndex.ToString();
}
}
}
CacheManager Net Core Redis 缓存的更多相关文章
- c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具
c#实例化继承类,必须对被继承类的程序集做引用 0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 使用Redis缓存数据
上一篇文章(https://www.cnblogs.com/meowv/p/12943699.html)完成了项目的全局异常处理和日志记录. 在日志记录中使用的静态方法有人指出写法不是很优雅,遂优化一 ...
- ASP.NET Core教程:ASP.NET Core中使用Redis缓存
参考网址:https://www.cnblogs.com/dotnet261010/p/12033624.html 一.前言 我们这里以StackExchange.Redis为例,讲解如何在ASP.N ...
- Redis 缓存 + Spring 的集成示例(转)
<整合 spring 4(包括mvc.context.orm) + mybatis 3 示例>一文简要介绍了最新版本的 Spring MVC.IOC.MyBatis ORM 三者的整合以及 ...
- springboot redis 缓存对象
只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile(" ...
- Spring Boot 使用Redis缓存
本文示例源码,请看这里 Spring Cache的官方文档,请看这里 缓存存储 Spring 提供了很多缓存管理器,例如: SimpleCacheManager EhCacheCacheManager ...
- Spring集成Redis缓存
作者:13 GItHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...
- spring boot 2.0.4 Redis缓存配置
spring boot 2 使用RedisTemplate操作redis存取对象时,需要先进行序列化操作 import org.springframework.cache.CacheManager; ...
- spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
前言 开心一刻 小白问小明:“你前面有一个5米深的坑,里面没有水,如果你跳进去后该怎样出来了?”小明:“躺着出来呗,还能怎么出来?”小白:“为什么躺着出来?”小明:“5米深的坑,还没有水,跳下去不死就 ...
随机推荐
- C++改变数组长度
C++改变数组长度 代码 //改变数组长度 #ifndef CHANGELENGTH1D_H #define CHANGELENGTH1D_H #include<stdexcept> #i ...
- 用rewrite规则实现将所有到a域名的访问rewrite到b域名
1.临时重定向 1.1使用redirect实现临时重定向 # cat /apps/nginx/conf/nginx.conf ...省略... server { listen 80; server_n ...
- 无xml文件的springMVC
使用springMVC我们一般都会在web.xml中配置一个dispatcher,现在我们基于用java代码的方式来使用springMVC import org.springframework.con ...
- GIL解释器锁 & 进程池与线程池
今日内容 GIL 全局解释器锁(重要理论) 验证 GIL 的存在及功能 验证 python 多线程是否有用 死锁现象 进程池与线程池(使用频率高) IO模型 详细参考: https://www.bil ...
- Solution -「HDU 6643」Ridiculous Netizens
\(\mathcal{Description}\) Link. 给定一棵含有 \(n\) 个结点的树,点 \(u\) 有点权 \(w_u\),求树上非空连通块的数量,使得连通块内点权积 \(\ ...
- shell脚本批量配置多台主机静态ip
关于脚本 服务器使用之前,都需要先配置静态IP,那就将这种简单重复的工作,交给脚本来处理吧,让我们运维有更多的时间喝茶看报刷微博 脚本使用 sh ssh.sh ip.txt ssh.sh 为脚本的名称 ...
- 日行一算(Consecutive Integer-连续整数)
题目 题目描述 2005年的百度之星初赛有这么一道题,一个正整数有可能可以被表示为 m(m>1) 个连续正整数之和,如: 15=1+2+3+4+5 15=4+5+6 15=7+8 但现在你的任务 ...
- Linux安装Python xlrd、xlwt、xlutils模块
一.安装setuptools: 可以先打开setuptools的python官网看看setuptools软件包如何安装: 1 > wget https://bitbucket.org/pypa/ ...
- 攻防世界 MOBILE RemeberOther
解题思路: 下载后解压可以得到一个apk文件和word文件.查看word文件,里面写了比较简单的一句话,未获有有效信息.(后续会讲到这个word文档的使用) word内容 运行apk文件,如图: ap ...
- linux下使用fcrackzip来暴力破解zip压缩包
我是在kali上安装的,用命令sudo apt-get install fcrackzip 现在做一个例子,首先生成一个带有密码的zip的包 zip -P hujhh test.zip test1.t ...