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属性的配置元件? ...
随机推荐
- java DecimalFormat类
今天去面试了,需要上机做题.题目的内容是计算一个货物订单的税费和总价格(包括税费),结果需要精确到两个小数,同时按照如下规则进行处理: 3.01 ——>3.05, 2.48——> ...
- uwsgi的python2+3多版本共存实操使用virtualenv
1首先,机器需要有python2和python3的可执行环境.确保pip和pip3命令可用.原理就是在哪个环境下安装uwsgi.uwsgi启动的时候,就用的哪个python版本 2安装virtuale ...
- React-Todos
最近学完React的最基本概念,闲下来的时候就自己写了一个Todo-List的小应用.这里做个简略的说明,给想好好学React的新手看. React-Todo 学习前提 这里我用了webpackb做了 ...
- python学习-day 1
Python开发IDE(工具)Pycharm.eclipse1.循环while 条件 #循环体 #条件为真则执行 #条件为假则执行break用于退出所有循环continue用于退出当前循环 2.Pyc ...
- WPF DatePicker 默认显示当前时间
两种方法: 1.通过后台赋值: this.datePicker.SelectedDate = DateTime.Now; 2.前台控件的属性直接赋值 xmlns:sys="clr-names ...
- HTML5 APP
jquery,bootstrap http://www.bcty365.com/content-74-2640-1.html JS运行效率,浏览器 html+js+css3 css3是在css的 ...
- 设置myeclipse文件的打开格式
- [jOOQ中文]2. jOOQ与Spring和Druid整合
https://segmentfault.com/a/1190000010496053 jOOQ和Spring很容易整合. 在这个例子中,我们将整合: Alibaba Druid(但您也可以使用其他连 ...
- IDEA 实用注册码自动生成
将以下代码保存成keygen.java java开发者一看就明白什么意思! import java.math.BigInteger; import java.util.Date; import jav ...
- @@ERROR和@@ROWCOUNT的用法
1. @ERROR 当前一个语句遇到错误,则返回错误号,否则返回0.需要注意的是@ERROR在每一条语句执行后会被立刻重置,因此应该在要验证的语句执行后检查数值或者是将它保存到局部变量 ...