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米深的坑,还没有水,跳下去不死就 ...
随机推荐
- SpringAOP/切面编程示例
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11833954.html Spring AOP/切面编程实例和一些注意事项, 主要是利用注解来实 ...
- jdk1.5新特性之-------静态导入
import java.util.ArrayList; import java.util.Collections; /* jdk1.5新特性之-------静态导入 静态导入的作用: 简化书写. 静态 ...
- IP地址与子网划分
IP地址与子网划分 目录 IP地址与子网划分 一.IP地址(Internet Protocol Address) 1.IP地址的表示 2.IP地址的组成 3.IP地址的分类 (1)A类IP地址 (2) ...
- 猪齿鱼平台常用前端css实现方案
居中 最常用的height + line-height,以及margin:0 auto的居中方式就不再阐述,以下介绍两种容错性高的实现方案. flex布局实现 猪齿鱼前端日常开发中,我们多以fle ...
- Byobu安装与使用
机子为Ubuntu18 Byobu安装 sudo apt-get install byobu Byobu安装后默认禁用,需要启用Byobu,之后每次登陆自动启用Byobu byobu-enable 还 ...
- SQLServer误删ReportServerTempDB导致打不开其他库——解决方案
无意间删除了ReportServerTempDB库和ReportServer 导致其他库连接不上 错误:报表服务器无法打开与报表服务器数据库的连接.所有请求和处理都要求与数据库建立连接. 解决方案: ...
- opencv笔记--ORB
ORB detector 使用 FAST detector 和 BRIEF descriptor 基本思路.在介绍 ORB 之前,首先对 FAST 与 BRIEF 进行说明. 1 FAST FAST( ...
- Solution -「ZJOI 2016」「洛谷 P3352」线段树
\(\mathcal{Descrtiption}\) 给定 \(\{a_n\}\),现进行 \(m\) 次操作,每次操作随机一个区间 \([l,r]\),令其中元素全部变为区间最大值.对于每个 \ ...
- Solution -「LOJ #141」回文子串 ||「模板」双向 PAM
\(\mathcal{Description}\) Link. 给定字符串 \(s\),处理 \(q\) 次操作: 在 \(s\) 前添加字符串: 在 \(s\) 后添加字符串: 求 \(s\ ...
- Hive之同比环比的计算
Hive系列文章 Hive表的基本操作 Hive中的集合数据类型 Hive动态分区详解 hive中orc格式表的数据导入 Java通过jdbc连接hive 通过HiveServer2访问Hive Sp ...