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. 微擎site.php函数以及路由连接

    任务1: 微擎模块设计: module.php 规则类: 会调用module.php manifest.xml中业务菜单对应的模块 如果在site.php中没有相应的函数 比如 /web/index. ...

  2. TCP的报文详细解读

    这张图好像挺有名的,其实一开始我看见的时候是一脸懵逼的,但是通过翻书(大学时代最害怕的计算机网络),查阅他人博客等等办法,最后终于有了一个系统的了解,当然,这里知识点多而杂,大家可以多看几遍,结合上面 ...

  3. 【AGC035D】Add and Remove(脑洞 DP 分治)

    题目链接 大意 给出\(N\)个数的序列,每次操作可以选择连续的三个数,将中间的那个数抽出,将另外两个数的数值加上中间那个数的数值. 一直执行以上操作直到只剩最后两个数,求最后两个数的所有可能的和的最 ...

  4. Solution -「AGC 036D」「AT 5147」Negative Cycle

    \(\mathcal{Descriprtion}\)   Link.   在一个含 \(n\) 个结点的有向图中,存在边 \(\lang i,i+1,0\rang\),它们不能被删除:还有边 \(\l ...

  5. Solution -「多校联训」染色

    \(\mathcal{Description}\)   Link.   给定 \(n\) 和 \(q\) 次询问,每次询问给出 \(x,k\),求第 \(x\) 位为 0 且任意两个 1 的下标之差不 ...

  6. Solution -「CF 1056G」Take Metro

    \(\mathcal{Description}\)   Link.   有 \(n\) 个站台在一个圆环上,顺时针编号 \(1\sim n\),其中 \(1\sim m\) 号站台只能乘坐顺时针转的环 ...

  7. 简单的springboot + vue

    安装vue 脚手架 npm install -g @vue/cli 查看vue 版本 vue -V 创建vue项目 vue create vue_project Vue CLI v4.5.13? Pl ...

  8. react 也就这么回事 02 —— JSX 插值表达式、条件渲染以及列表渲染

    我们已经学会了 React 创建元素和渲染元素 ReactDOM.render(<div>Hello React!</div>, document.getElementById ...

  9. apache缺少模块解决方法

    找到一台老古董机器 [root@resource conf]# cat /etc/redhat-release CentOS release 5.6 (Final) [root@resource co ...

  10. 传输层隧道技术之lcx内网端口转发

    传输层技术包括TCP隧道.UDP隧道和常规端口转发等.在渗透测试中,如果内网防火墙阻止了指定端口的访问,在获得目标机器的权限后,可以使用IPTABLES打开指定端口.如果内网中存在一系列防御系统,TC ...