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属性的配置元件? ...
随机推荐
- map、reduce处理数据结构及常见案例
随着三大前端框架和小程序的流行,MVVM大行其道,而其中的核心是 ViewModel 层,它就像是一个中转站(value converter),负责转换 Model 中的数据对象来让数据变得更容易管理 ...
- 熟练使用Linux进程管理类命令
进程管理类命令 – ps命令 ps命令主要用于查看系统的进程 该命令的语法为:ps [参数] ps命令的常用参数选项有: -a:显示当前控制终端的进程(包含其他用户的). -u:显示进程的用户名和启动 ...
- Oracle11g 搭建单实例DataGuard (转载)
原文:http://blog.itpub.net/29324876/viewspace-1246133/ 环境:主备库都为单实例并且数据库SID相同 OS:red hat 6.5 Oracle:11. ...
- Solr分组聚合查询之Group
摘要: Solr对结果的分组处理除了facet还可以使用group.Solr的group是根据某一字段对结果分组,将每一组内满足查询的结果按顺序返回. Group对比Facet Group和Facet ...
- selenium+python在mac环境上的搭建
前言 mac自带了python2.7的环境,所以在mac上安装selenium环境是非常简单的,输入2个指令就能安装好 需要安装的软件: 1.pip 2.selenium2.53.6 3.Firefo ...
- oracle执行sql文件
oracle执行sql文件 在PL/SQL中直接用command window执行就可以了: PL/SQL developer----->File------>New---->com ...
- jsp页面获取地址栏中的参数
- Node.js究竟是什么?
来源:https://www.ibm.com/developerworks/cn/opensource/os-nodejs/index.html?ca=drs#ibm-pcon Node 旨在解决 ...
- 使用java代码编辑oracle数据库
package com.hanqi; import java.io.IOException; import java.sql.Connection; import java.sql.DriverMan ...
- 27.Remove Element(Array)
Given an array and a value, remove all instances of that value in place and return the new length. T ...