sitecore 缓存管理器
namespace XXX.Shared.Infrastructure.Caching
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Threading;
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.Sites; /// <summary>
/// The cache manager.
/// </summary>
public sealed class CacheManager
{
#region Static Fields private static readonly Lazy<CacheManager> Instance = new Lazy<CacheManager>(() => new CacheManager(), LazyThreadSafetyMode.ExecutionAndPublication); private static readonly object SyncLock = new object(); #endregion #region Fields private readonly ObjectCache cache = MemoryCache.Default; #endregion #region Public Properties /// <summary>
/// Gets the current.
/// </summary>
public static CacheManager Current
{
get { return Instance.Value; }
} #endregion #region Public Methods and Operators /// <summary>
/// The add.
/// </summary>
/// <param name="key">
/// The key.
/// </param>
/// <param name="data">
/// The data.
/// </param>
/// <param name="expiryTimeInHours">
/// </param>
/// <typeparam name="T">
/// </typeparam>
public void Add<T>(string key, T data, double expiryTimeInHours = ) where T : class
{
try
{
if (data == null)
{
throw new ArgumentNullException("data", "Cannot add null to the cache.");
} lock (SyncLock)
{
var policy = new CacheItemPolicy
{
AbsoluteExpiration = DateTime.Now.AddHours(expiryTimeInHours),
Priority = CacheItemPriority.Default,
SlidingExpiration = TimeSpan.Zero
}; this.cache.Add(key, data, policy);
}
}
catch (Exception ex)
{
Log.Debug(ex.Message, this);
}
} /// <summary>
/// The contains key.
/// </summary>
/// <param name="key">
/// The key.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool ContainsKey(string key)
{
lock (SyncLock)
{
return this.cache.Contains(key);
}
} /// <summary>
/// The get.
/// </summary>
/// <param name="key">
/// The key.
/// </param>
/// <typeparam name="T">
/// </typeparam>
/// <returns>
/// The <see cref="T"/>.
/// </returns>
public T Get<T>(string key) where T : class
{
lock (SyncLock)
{
return this.ContainsKey(key) ? this.cache.Get(key) as T : default(T);
}
} /// <summary>
/// The get all cache keys.
/// </summary>
/// <returns>
/// The <see cref="IEnumerable{T}"/>.
/// </returns>
public IEnumerable<string> GetAllCacheKeys()
{
return this.cache.Select(item => item.Key).ToList();
} /// <summary>
/// The purge.
/// </summary>
public void Purge()
{
lock (SyncLock)
{
var keys = this.cache.Select(item => item.Key); keys.ToList().ForEach(this.Remove);
}
} /// <summary>
/// The remove.
/// </summary>
/// <param name="key">
/// The key.
/// </param>
public void Remove(string key)
{
lock (SyncLock)
{
if (!this.ContainsKey(key))
{
return;
} this.cache.Remove(key);
}
} public void RemoveSitecoreItemCache(string id, string siteName)
{
if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(siteName))
{
using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext(siteName)))
{
var db = SiteContext.Current.Database; if (db != null)
{
db.Caches.ItemCache.RemoveItem(new ID(id)); db.Caches.DataCache.RemoveItemInformation(new ID(id)); db.Caches.StandardValuesCache.RemoveKeysContaining(id);
}
}
}
} /// <summary>
/// The remove by prefix.
/// </summary>
/// <param name="prefix">
/// The prefix.
/// </param>
public void RemoveByPrefix(string prefix)
{
lock (SyncLock)
{
var keys = this.cache.Where(item => item.Key.IndexOf(prefix, StringComparison.OrdinalIgnoreCase) != -); keys.ToList().ForEach(item => this.Remove(item.Key));
}
} #endregion
}
}
sitecore 缓存管理器的更多相关文章
- 自定义缓存管理器 或者 Spring -- cache
Spring Cache 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个简单的例子进行展开,通过对比我们原来的自定义缓存和 spring 的基于注释的 c ...
- SpringMVC + Mybatis + Shiro + ehcache时缓存管理器报错。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' ...
- [转载]挂接缓存管理器CcMapData()实现文件XX
原作者Azy,发表于DebugMan论坛. ======================================================= 这个方法的最大好处在于简单~~不用分别处理~ ...
- Spring自定义缓存管理及配置Ehcache缓存
spring自带缓存.自建缓存管理器等都可解决项目部分性能问题.结合Ehcache后性能更优,使用也比较简单. 在进行Ehcache学习之前,最好对Spring自带的缓存管理有一个总体的认识. 这篇文 ...
- Apache-Shiro+Zookeeper系统集群安全解决方案之缓存管理
上篇[Apache-Shiro+Zookeeper系统集群安全解决方案之会话管理],解决了Shiro在系统集群开发时安全的会话共享问题,系统在使用过程中会有大量的权限检查和用户身份检验动作,为了不频繁 ...
- HTTP属性管理器详解
1)HTTP Cache Manager 2)HTTP Cookie 管理器 3)HTTP 信息头管理器 4)HTTP 授权管理器 5)HTTP 请求默认值 为什么会有这些http属性的配置元件? ...
- shiro缓存管理
一. 概述 Shiro作为一个开源的权限框架,其组件化的设计思想使得开发者可以根据具体业务场景灵活地实现权限管理方案,权限粒度的控制非常方便.首先,我们来看看Shiro框架的架构图:从上图我们可以很清 ...
- [Abp 源码分析]八、缓存管理
0.简介 缓存在一个业务系统中十分重要,常用的场景就是用来储存调用频率较高的数据.Abp 也提供了一套缓存机制供用户使用,在使用 Abp 框架的时候可以通过注入 ICacheManager 来新建/设 ...
- HTTP属性管理器 初探
1)HTTP Cache Manager 2)HTTP Cookie 管理器 3)HTTP 信息头管理器 4)HTTP 授权管理器 5)HTTP 请求默认值 为什么会有这些http属性的配置元件? ...
随机推荐
- Spring Mvc:用MultiPartFile上传单个文件,多个文件
1.单个文件上传步骤: 添加Apache文件上传jar包 首先需要下载两个apache上传文件的jar包,commons-fileupload-1.3.1jar,commons-io-2.4.jar ...
- [转][ASP.net]后台页面刷新
三种后台刷新页面的方法: // 刷新方法一 Response.AddHeader("); // 刷新方法二 Response.Write("<script language= ...
- IDEA无法下载plugin的解决办法
有些时候我们在用IDEA安装plugins的时候,会因为各种原因搜索不到想要的依赖,或者搜索到却无法安装,针对这个问题,现在这里有两种方法可以尝试一下. 第一种: 找到settings->sys ...
- J.U.C 整体认识
深入浅出 Java Concurrency (1) : J.U.C的整体认识 去年年底有一个Guice的研究计划,可惜由于工作“繁忙”加上实际工作中没有用上导致“无疾而终”,最终只是完成了Guice的 ...
- hadoop学习day1环境配置笔记(非完整流程)
hdfs的工作机制: 1.客户把一个文件存入hdfs,其实hdfs会把这个文件切块后,分散存储在N台linux机器系统中(负责存储文件块的角色:data node)<准确来说:切块的行为是由客户 ...
- django-form.errors和前端上传文件
一.上传文件: 在相应的模型里面定义`FileField`或者是`ImageField`类型的字段,并且1.设置好`upload_to`参数来指定上传的路径. class User(models.Mo ...
- oracle实例侦听
在oracle服务器端命令行中执行 C:\>lsnrctl进入到 LISNRCTL>界面 键入help字符会回显相关的帮助命令
- 公共文件模块include
首先新建一个include 把所有引入的文件放入公共文件里 function getBaseURL() { var pathName = window.document.location.pathna ...
- tomcat限制ip访问
context元素添加 <Context> <Valve className="org.apache.catalina.valves.RemoteAddrValve&quo ...
- 323. Number of Connected Components in an Undirected Graph按照线段添加的并查集
[抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...