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 缓存管理器的更多相关文章

  1. 自定义缓存管理器 或者 Spring -- cache

    Spring Cache 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个简单的例子进行展开,通过对比我们原来的自定义缓存和 spring 的基于注释的 c ...

  2. SpringMVC + Mybatis + Shiro + ehcache时缓存管理器报错。

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' ...

  3. [转载]挂接缓存管理器CcMapData()实现文件XX

    原作者Azy,发表于DebugMan论坛. ======================================================= 这个方法的最大好处在于简单~~不用分别处理~ ...

  4. Spring自定义缓存管理及配置Ehcache缓存

    spring自带缓存.自建缓存管理器等都可解决项目部分性能问题.结合Ehcache后性能更优,使用也比较简单. 在进行Ehcache学习之前,最好对Spring自带的缓存管理有一个总体的认识. 这篇文 ...

  5. Apache-Shiro+Zookeeper系统集群安全解决方案之缓存管理

    上篇[Apache-Shiro+Zookeeper系统集群安全解决方案之会话管理],解决了Shiro在系统集群开发时安全的会话共享问题,系统在使用过程中会有大量的权限检查和用户身份检验动作,为了不频繁 ...

  6. HTTP属性管理器详解

      1)HTTP Cache Manager 2)HTTP Cookie 管理器 3)HTTP 信息头管理器 4)HTTP 授权管理器 5)HTTP 请求默认值 为什么会有这些http属性的配置元件? ...

  7. shiro缓存管理

    一. 概述 Shiro作为一个开源的权限框架,其组件化的设计思想使得开发者可以根据具体业务场景灵活地实现权限管理方案,权限粒度的控制非常方便.首先,我们来看看Shiro框架的架构图:从上图我们可以很清 ...

  8. [Abp 源码分析]八、缓存管理

    0.简介 缓存在一个业务系统中十分重要,常用的场景就是用来储存调用频率较高的数据.Abp 也提供了一套缓存机制供用户使用,在使用 Abp 框架的时候可以通过注入 ICacheManager 来新建/设 ...

  9. HTTP属性管理器 初探

      1)HTTP Cache Manager 2)HTTP Cookie 管理器 3)HTTP 信息头管理器 4)HTTP 授权管理器 5)HTTP 请求默认值 为什么会有这些http属性的配置元件? ...

随机推荐

  1. python3调用阿里云短信服务

    #!/usr/bin/env python#-*- coding:utf-8 -*-#Author:lzd import uuidimport datetimeimport hmacimport ba ...

  2. js复制URL链接

    html: <div style="height:0px; text-indent:-10000px;"><span id="hdcopyurl&quo ...

  3. php错误:Uncaught exception com_exception with message Failed to create COM object

    本文为大家讲解的是php错误:Uncaught exception com_exception with message Failed to create COM object,感兴趣的同学参考下. ...

  4. SmallLocks

    folly/SmallLocks.h This module is currently x64 only. This header defines two very small mutex types ...

  5. user_add示例

    #!/usr/bin/python3# -*- coding: utf-8 -*-# @Time    : 2018/5/28 16:51# @File    : use_test_add.py 数据 ...

  6. CSS控制文字只显示一行 超出部分显示省略号

         <p style="width: 120px; height: 50px; border: 1px solid blue; white-space: nowrap; over ...

  7. vue轮播(完整详细版)

    轮播组件vue <swiper :options="swiperOption" class='swiper-box'>     <swiper-slide v-f ...

  8. Sql语句在线转java bean https://www.bejson.com/othertools/sql2pojo/

    https://www.bejson.com/othertools/sql2pojo/

  9. Basic64 编码解码

    import sun.misc.BASE64Decoder; public class Base64 { /** * 字符串转Base64编码 * @param s * @return */ publ ...

  10. C#开发微信公众化平台

     C#开发微信公众化平台 写在前面 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文.菜单事件响应) 示例Demo下载 后记 最近公司在做微信开发,其实就是接口开发,网上 ...