个人项目框架搭建 -- 缓存接口与实现
1、缓存接口
using System; using System.Collections.Generic; using System.Runtime.Caching; using System.Text.RegularExpressions;namespace EnterpriseFrame.Core.Caching {/// <summary>/// Represents a manager for caching between HTTP requests (long term caching)/// </summary>public partial class MemoryCacheManager : ICacheManager{protected ObjectCache Cache{get{return MemoryCache.Default;}}/// <summary>/// Gets or sets the value associated with the specified key./// </summary>/// <typeparam name="T">Type</typeparam>/// <param name="key">The key of the value to get.</param>/// <returns>The value associated with the specified key.</returns>public virtual T Get<T>(string key){return (T)Cache[key];}/// <summary>/// Adds the specified key and object to the cache./// </summary>/// <param name="key">key</param>/// <param name="data">Data</param>/// <param name="cacheTime">Cache time</param>public virtual void Set(string key, object data, int cacheTime){if (data == null)return;var policy = new CacheItemPolicy();policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);Cache.Add(new CacheItem(key, data), policy);}/// <summary>/// Gets a value indicating whether the value associated with the specified key is cached/// </summary>/// <param name="key">key</param>/// <returns>Result</returns>public virtual bool IsSet(string key){return (Cache.Contains(key));}/// <summary>/// Removes the value with the specified key from the cache/// </summary>/// <param name="key">/key</param>public virtual void Remove(string key){Cache.Remove(key);}/// <summary>/// Removes items by pattern/// </summary>/// <param name="pattern">pattern</param>public virtual void RemoveByPattern(string pattern){var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);var keysToRemove = new List<String>();foreach (var item in Cache)if (regex.IsMatch(item.Key))keysToRemove.Add(item.Key);foreach (string key in keysToRemove){Remove(key);}}/// <summary>/// Clear all cache data/// </summary>public virtual void Clear(){foreach (var item in Cache)Remove(item.Key);}} }
2、扩展
using System;namespace EnterpriseFrame.Core.Caching {/// <summary>/// Extensions/// </summary>public static class CacheExtensions{/// <summary>/// 获取缓存,不存在则将acquire的结果存入缓存,默认时间60分钟/// </summary>/// <typeparam name="T">Type</typeparam>/// <param name="cacheManager">Cache manager</param>/// <param name="key">Cache key</param>/// <param name="acquire">Function to load item if it's not in the cache yet</param>/// <returns>Cached item</returns>public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire){return Get(cacheManager, key, 60, acquire);}/// <summary>/// 获取缓存,如果不存在缓存中,执行结果然后将其加载和缓存/// </summary>/// <typeparam name="T">缓存类型</typeparam>/// <param name="cacheManager">Cache manager</param>/// <param name="key">Cache key</param>/// <param name="cacheTime">缓存时间 在0分钟-不要缓存</param>/// <param name="acquire">没有缓存则执行此表达式设置缓存</param>/// <returns>Cached item</returns>public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, Func<T> acquire) {if (cacheManager.IsSet(key)){return cacheManager.Get<T>(key);}else{var result = acquire();if (cacheTime > 0)cacheManager.Set(key, result, cacheTime);return result;}}} }
3、MemoryCacheManager实现
using System; using System.Collections.Generic; using System.Runtime.Caching; using System.Text.RegularExpressions;namespace EnterpriseFrame.Core.Caching {/// <summary>/// Represents a manager for caching between HTTP requests (long term caching)/// </summary>public partial class MemoryCacheManager : ICacheManager{protected ObjectCache Cache{get{return MemoryCache.Default;}}/// <summary>/// Gets or sets the value associated with the specified key./// </summary>/// <typeparam name="T">Type</typeparam>/// <param name="key">The key of the value to get.</param>/// <returns>The value associated with the specified key.</returns>public virtual T Get<T>(string key){return (T)Cache[key];}/// <summary>/// Adds the specified key and object to the cache./// </summary>/// <param name="key">key</param>/// <param name="data">Data</param>/// <param name="cacheTime">Cache time</param>public virtual void Set(string key, object data, int cacheTime){if (data == null)return;var policy = new CacheItemPolicy();policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);Cache.Add(new CacheItem(key, data), policy);}/// <summary>/// Gets a value indicating whether the value associated with the specified key is cached/// </summary>/// <param name="key">key</param>/// <returns>Result</returns>public virtual bool IsSet(string key){return (Cache.Contains(key));}/// <summary>/// Removes the value with the specified key from the cache/// </summary>/// <param name="key">/key</param>public virtual void Remove(string key){Cache.Remove(key);}/// <summary>/// Removes items by pattern/// </summary>/// <param name="pattern">pattern</param>public virtual void RemoveByPattern(string pattern){var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);var keysToRemove = new List<String>();foreach (var item in Cache)if (regex.IsMatch(item.Key))keysToRemove.Add(item.Key);foreach (string key in keysToRemove){Remove(key);}}/// <summary>/// Clear all cache data/// </summary>public virtual void Clear(){foreach (var item in Cache)Remove(item.Key);}} }
摘自:NopCommerce框架(http://nopcommerce.codeplex.com/)
转载于:https://www.cnblogs.com/morang/p/5371651.html
个人项目框架搭建 -- 缓存接口与实现相关推荐
- SpringSecurity(二)、权限项目框架搭建
Springboot + SpringSecurity权限项目框架搭建 目录 一.项目介绍 二.项目搭建(父子工程) 1.添加 pom 依赖 2.修改 yml 配置 3.编写JwtTokenUtil工 ...
- spring cloud多模块项目框架搭建-集成SLF4J和log4j2日志组件
第七章 集成SLF4J和log4j2进行日志管理 本系列博客旨在搭建一套能用于实际开发使用的spring cloud多模块项目框架,并不是一个spring cloud的demo而已,提供分布式系统的 ...
- Android最好用的项目框架搭建
本篇先记录下当前项目中涉及的主要技术要点.也算是对所作项目的一次总结.如果这个过程能对你有些许的帮助,那可能就显得有意义点了. 一个完整的Android项目会涉及后台和前端.我们只关注于前端,也就是我 ...
- day18_项目框架搭建1
项目的工程目录: py_53 --根目录 py_api --子目录 day18_项目框架搭建 --项目工程目录 common -公用模块 excle.py -获取Excel表单的数据 handler_ ...
- spring cloud多模块项目框架搭建-Redis-Cluster集群搭建及系统集成
第九章 Redis-Cluster集群搭建及系统集成 本系列博客旨在搭建一套能用于实际开发使用的spring cloud多模块微服务项目框架,并不是一个spring cloud的demo而已,提供系统 ...
- spring cloud多模块项目框架搭建-集成lombok
第五章: spring cloud多模块项目框架搭建-集成lombok 本系列博客旨在搭建一套能用于实际开发使用的spring cloud多模块微服务项目框架,并不是一个spring cloud的de ...
- (三) Angular2项目框架搭建心得
前言: 在哪看到过angular程序员被React程序员鄙视,略显尴尬,确实Angular挺值得被调侃的,在1.*版本存在的几个性能问题,性能优化的"潜规则"贼多,以及从1.*到2 ...
- 【高校宿舍管理系统】第一章 建立数据库以及项目框架搭建
第一章 建立数据库以及项目框架搭建 提示:本博客个为人独立博客,不是权威,仅供参考!所有思路只做交流之用!如有不足之处,望各位在评论区友善指正. 文章目录 第一章 建立数据库以及项目框架搭建 前言 一 ...
- SpringBoot后端项目框架搭建
SpringBoot后端项目框架搭建 本节内容服务于SpringBoot + Vue 搭建 JavaWeb 增删改查项目. 工具安装 电脑已安装\配置如下工具: IDEA.jdk.MySQL及其可视化 ...
最新文章
- mysql子查询存到另一张表_MySQL数据库(11)----使用子查询实现多表查询
- (转载)浅析Hadoop文件格式
- 面试官:请实现一个通用函数把 callback 转成 promise
- Android 4.0 Launcher源码分析系列(二)
- 滴滴 KDD 2018 论文详解:基于强化学习技术的智能派单模型
- 性能测试——loadrunner_添加多个主机发送请求
- python学习(二)----字典
- 数字图像学笔记——8. 几种常见的空间滤波器(均值滤波器、中值滤波器)
- 【redies】五种数据类型
- kotlin的by lazy
- 第二章 蜕变!上古剑修!
- git clone https 克隆失败解决办法
- Github 热度飙升,一键生成最近抖音超火的 AI 人物绘图
- Tiny语言编译器简单介绍
- oracle 19c_windos_64位 百度云下载。WINDOWS.X64_193000_db_home.zip。不需要积分
- 【AD20学习笔记】PCB设计规则设置及手工布线
- 北京科技大学计算机专业毕设,北京科技大学毕业生近五年就业情况分析(2017)
- android归属地显示错误,Android开发【07-18疑问贴】求助大神 来电归属地拖移动问题...
- 当比你聪明的人还比你勤奋
- 商标买卖哪个平台最好_商标买卖哪个平台最大?