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 缓存的更多相关文章

  1. 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 ...

  2. 基于 abp vNext 和 .NET Core 开发博客项目 - 使用Redis缓存数据

    上一篇文章(https://www.cnblogs.com/meowv/p/12943699.html)完成了项目的全局异常处理和日志记录. 在日志记录中使用的静态方法有人指出写法不是很优雅,遂优化一 ...

  3. ASP.NET Core教程:ASP.NET Core中使用Redis缓存

    参考网址:https://www.cnblogs.com/dotnet261010/p/12033624.html 一.前言 我们这里以StackExchange.Redis为例,讲解如何在ASP.N ...

  4. Redis 缓存 + Spring 的集成示例(转)

    <整合 spring 4(包括mvc.context.orm) + mybatis 3 示例>一文简要介绍了最新版本的 Spring MVC.IOC.MyBatis ORM 三者的整合以及 ...

  5. springboot redis 缓存对象

    只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile(" ...

  6. Spring Boot 使用Redis缓存

    本文示例源码,请看这里 Spring Cache的官方文档,请看这里 缓存存储 Spring 提供了很多缓存管理器,例如: SimpleCacheManager EhCacheCacheManager ...

  7. Spring集成Redis缓存

    作者:13 GItHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...

  8. spring boot 2.0.4 Redis缓存配置

    spring boot 2 使用RedisTemplate操作redis存取对象时,需要先进行序列化操作 import org.springframework.cache.CacheManager; ...

  9. spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!

    前言 开心一刻 小白问小明:“你前面有一个5米深的坑,里面没有水,如果你跳进去后该怎样出来了?”小明:“躺着出来呗,还能怎么出来?”小白:“为什么躺着出来?”小明:“5米深的坑,还没有水,跳下去不死就 ...

随机推荐

  1. java短信接入

    1,注册一个中间公司的短信平台(比如网建) 2,找到密匙  3,找到链接案例 4,复制代码 下载jar包 import java.io.UnsupportedEncodingException;imp ...

  2. matlab构建栅格地图绘图思路

    matlab构建栅格地图绘图思路 近来因研究需要,调研并思考了栅格地图的生成方法,暂时总结以备不时之需. 栅格的建立最需要注意栅格粒度的问题,即根据需要调整栅格的边长,目前有两种思路一种是固定栅格边长 ...

  3. fuzz——AFL基础使用方法

    最近打 ctf 的时候感觉有点遇到瓶颈,就来 fuzz 这块看看. AFL 全称为 American huzzy loop,是 Fuzzing 最高级的测试工具之一.这个工具对有源码和无源码的二进制程 ...

  4. python篇第10天【For 循环语句】

      实例 #!/usr/bin/python # -*- coding: UTF-8 -*- for a in 'Henry': print "This is ", a   fru ...

  5. 帆软报表(finereport)JS实现cpt中详细单元格刷新

    1.刷新固定单元格  setInterval(function(){ //获取第二行第 5 列 E2 单元格对象 var _changeCell = $("tr[tridx=1]" ...

  6. 初见Redis

    Redis是什么,有什么特点和优势 Redis是一个开源用C语言编写的,基于内存,可以持久化,高性能的key-value数据库,并提供多种语言的API. 它也被称为数据结构服务器,因为值(value) ...

  7. Linux实现MySQL数据库凌晨自动备份

    Linux实现MySQL数据库凌晨自动备份 备份多数据库,每天凌晨两点执行,使用当前年月日作为文件夹,不存在该文件夹就创建,删除七天前备份过的文件. 定时调度使用crontab 1 login_use ...

  8. simulink模块执行顺序

    1.simulink各模块执行顺序 Simulink模块的执行顺序都是序贯进行的,也就是沿着信号的流向进行.没有输入的模块先进行计算,更新状态量与输出,需要输入信号的模块等到输入信号准备ready之后 ...

  9. Dubbo源码剖析六之SPI扩展点的实现之getExtensionLoader

    Dubbo SPI机制之三Adaptive自适应功能 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中,示例案例中自定义了扩展接口而不是使用Dubbo已提供的扩展接口.在案例中,主程序分 ...

  10. MyBatis功能点二应用:第三方分页插件使用

    pageHelper分⻚插件使用 在前面文章MyBatis功能点二:plugins插件使用 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中介绍了自定义插件的使用,本文介绍第三方插件pa ...