1 借鉴这篇文章

https://www.cnblogs.com/zuowj/p/8440902.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Caching;
using System.Diagnostics; namespace ConsoleApp1
{
public static class MemoryCacheHelper
{
private static readonly object _locker = new object();
private static readonly object _locker2 = new object(); /// <summary>
/// 根据key取缓存,不存在则返回null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T GetCacheItem<T>(String key)
{
try
{
return (T)MemoryCache.Default[key];
}
catch (Exception ex)
{ return default(T);
}
} /// <summary>
/// 是否包含指定键的缓存项
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Contains(string key)
{
return MemoryCache.Default.Contains(key);
} public static T GetOrAddCacheItem<T>(string key, Func<T> cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null)
{
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("Invalid cache key");
}
if (cachePopulate == null)
{
throw new ArgumentException("cachePopulate");
}
if (slidingExpiration == null && absoluteExpiration == null)
{
throw new ArgumentException("Either a sliding expiration or absolute must be provided");
}
if (MemoryCache.Default[key] == null)
{
lock (_locker)
{
if (MemoryCache.Default[key] == null)
{
T cacheValue = cachePopulate();
if (!typeof(T).IsValueType && ((object)cacheValue) == null)
{
return cacheValue;
}
var item = new CacheItem(key, cacheValue);
var policy = CreatePolicy(slidingExpiration, absoluteExpiration);
MemoryCache.Default.Add(item, policy);
} }
} return (T)MemoryCache.Default[key];
} public static T GetOrAddCacheItem<T>(string key,Func<T> cachePopulate,string dependencyFilePath)
{
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("Invalid cache key");
}
if (cachePopulate == null)
{
throw new ArgumentException("cachePopulate");
} if (MemoryCache.Default[key]==null)
{
lock (_locker2)
{
if (MemoryCache.Default[key] == null)
{
T cacheValue = cachePopulate();
if (!typeof(T).IsValueType && ((object)cacheValue) == null)
{
return cacheValue;
}
var item = new CacheItem(key, cacheValue);
var policy = CreatePolicy(dependencyFilePath);
MemoryCache.Default.Add(item, policy);
}
}
} return (T)MemoryCache.Default[key];
} /// <summary>
/// 移除指定键的缓存项
/// </summary>
/// <param name="key"></param>
public static void RemoveCacheItem(string key)
{
try
{
MemoryCache.Default.Remove(key);
}
catch (Exception)
{ }
} private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration)
{
var policy = new CacheItemPolicy();
if (slidingExpiration.HasValue)
{
policy.SlidingExpiration = slidingExpiration.Value;
}
else if (absoluteExpiration.HasValue)
{
policy.AbsoluteExpiration = absoluteExpiration.Value;
} policy.Priority = CacheItemPriority.Default;
return policy;
} /// <summary>
/// 缓存文件
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
private static CacheItemPolicy CreatePolicy(string filepath)
{
var policy = new CacheItemPolicy();
policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List<string>() { filepath })); policy.Priority = CacheItemPriority.Default;
return policy;
}
}
}

memoryCache的使用的更多相关文章

  1. 【无私分享:ASP.NET CORE 项目实战(第十一章)】Asp.net Core 缓存 MemoryCache 和 Redis

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 经过 N 久反复的尝试,翻阅了网上无数的资料,GitHub上下载了十几个源码参考, Memory 和 Redis 终于写出一个 ...

  2. 基于MemoryCache的缓存辅助类

    背景: 1. 什么是MemoryCache? memoryCache就是用电脑内存做缓存处理 2.使用范围? 可用于不常变的数据,进行保存在内存中,提高处理效率 代码: /// <summary ...

  3. 从零开始,搭建博客系统MVC5+EF6搭建框架(3),添加Nlog日志、缓存机制(MemoryCache、RedisCache)、创建控制器父类BaseController

    一.回顾系统进度以及本章概要 目前博客系统已经数据库创建.以及依赖注入Autofac集成,接下来就是日志和缓存集成,这里日志用的是Nlog,其实还有其他的日志框架如log4,这些博客园都有很多介绍,这 ...

  4. Android开源框架:Universal-Image-Loader解析(二)MemoryCache

  5. .NET 4.0 MemoryCache with SqlChangeMonitor

    Summary There isn't a lot of documentation on the internet about how to use the SqlChangeMonitor wit ...

  6. 缓存管理Memorycache 的使用

      前言:什么是memoryCache? 一种缓存管理技术,某些只读数据频繁操作数据库,会对系统的性能有很大的开销,所以我们使用缓存技术,当数据库内容更新,我们在更更新缓存的数据值.目前缓存讲技术的产 ...

  7. Asp.net Core 缓存 MemoryCache 和 Redis

    Asp.net Core 缓存 MemoryCache 和 Redis 目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 经过 N 久反复的尝试,翻阅了网上无数的资料,GitH ...

  8. 在.NET项目中使用PostSharp,使用MemoryCache实现缓存的处理(转)

    在之前一篇随笔<在.NET项目中使用PostSharp,实现AOP面向切面编程处理>介绍了PostSharp框架的使用,试用PostSharp能给我带来很多便利和优势,减少代码冗余,提高可 ...

  9. 在.NET项目中使用PostSharp,使用MemoryCache实现缓存的处理

    在之前一篇随笔<在.NET项目中使用PostSharp,实现AOP面向切面编程处理>介绍了PostSharp框架的使用,试用PostSharp能给我带来很多便利和优势,减少代码冗余,提高可 ...

  10. .NET Core 2.0迁移技巧之MemoryCache问题修复

    对于传统的.NET Framework项目而言,System.Runtime.Caching命名空间是常用的工具了,其中MemoryCache类则常被用于实现内存缓存. .NET Core 2.0暂时 ...

随机推荐

  1. Z-Score数据标准化(转载)

    简介Z-Score标准化是数据处理的一种常用方法.通过它能够将不同量级的数据转化为统一量度的Z-Score分值进行比较. 一句话解释版本: Z-Score通过(x-μ)/σ将两组或多组数据转化为无单位 ...

  2. C# 使用 Dapper 实现 SQLite 增删改查

    Dapper 是一款非常不错的轻型 ORM 框架,使用起来非常方便,经常使用 EF 框架的人几乎感觉不到差别,下面是自己写的 Sqlite 通用帮助类: 数据连接类: public class SQL ...

  3. Python基础教程(第2版 修订版) pdf

    Python基础教程(第2版 修订版) 目录 D11章快速改造:基础知识11.1安装Python11.1.1Windows11.1.2Linux和UNIX31.1.3苹果机(Macintosh)41. ...

  4. IDEA 多模块工程相互依赖

    最近为了结构项目,抽离通用模块,同时使用一个工程管理所有模块,使用了多模块工程.不过在依赖其他模块的编译上出现了问题,总是报找不到被依赖的jar包. 最后的解决办法也很简单,对于被依赖的模块,要在ma ...

  5. MySQL修改表名示例

    首先,我们新建一个名为test_table的表: drop table if exists test_table; create table test_table select TABLE_SCHEM ...

  6. pt-kill MySQL会话杀灭神器

    废话不多说,直接上例子: pt-kill --host=127.0.0.1 --user=xxx --password=xxxxxx --port=xxxx --busy-time 10 --matc ...

  7. C# 代码自动生成工具

    开源:C# 代码自动生成工具,支持站点前后台   前言 写这个项目有很长一段时间了,期间也修修改改,写到最后,自己也没咋用(研究方向变化了). 正文 具体项目开源了:https://github.co ...

  8. C++ 顺序容器(vector,list、deque,stack,queue)

    顺序容器的种类有:vector,list.deque 顺序容器适配器: stack     //先进后出   栈 queue   //先进先出   队列 priority_queue   //也优先管 ...

  9. springboot的mapper.xml在src下问题

    在pom.xml里面的build标签加上resources说明 <resources> <!-- mapper.xml文件在java目录下 --> <resource&g ...

  10. poj3660(floyd最短路)

    题目链接:https://vjudge.net/problem/POJ-3660 题意:给出一个有向图,n个结点,每个结点的权值为[1,n]中的一个独特数字,m条边,如果存在边a->b,说明a的 ...