nopCommerce_3.00-Nop.Core.Caching
namespace Nop.Core.Caching
{
/// <summary>
/// Cache manager interface
/// </summary>
public interface ICacheManager
{
/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
T Get<T>(string key); /// <summary>
/// Adds the specified key and object to the cache.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
void Set(string key, object data, int cacheTime); /// <summary>
/// Gets a value indicating whether the value associated with the specified key is cached
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
bool IsSet(string key); /// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
void Remove(string key); /// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
void RemoveByPattern(string pattern); /// <summary>
/// Clear all cache data
/// </summary>
void Clear();
}
}
namespace Nop.Core.Caching
{
/// <summary>
/// Represents a NopNullCache
/// </summary>
public partial class NopNullCache : ICacheManager
{
/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
public T Get<T>(string key)
{
return default(T);
} /// <summary>
/// Adds the specified key and object to the cache.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
public void Set(string key, object data, int cacheTime)
{
} /// <summary>
/// Gets a value indicating whether the value associated with the specified key is cached
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
public bool IsSet(string key)
{
return false;
} /// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
public void Remove(string key)
{
} /// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
public void RemoveByPattern(string pattern)
{
} /// <summary>
/// Clear all cache data
/// </summary>
public void Clear()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Runtime.Caching;
using System.Text.RegularExpressions; namespace Nop.Core.Caching
{
/// <summary>
/// Represents a MemoryCacheCache
/// </summary>
public partial class MemoryCacheManager : ICacheManager
{
protected ObjectCache Cache
{
get
{
return MemoryCache.Default;
}
} /// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
public T Get<T>(string key)
{
return (T)Cache[key];
} /// <summary>
/// Adds the specified key and object to the cache.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
public void Set(string key, object data, int cacheTime)
{
if (data == null)
return; var policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
Cache.Add(new CacheItem(key, data), policy);
} /// <summary>
/// Gets a value indicating whether the value associated with the specified key is cached
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
public bool IsSet(string key)
{
return (Cache.Contains(key));
} /// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
public void Remove(string key)
{
Cache.Remove(key);
} /// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
public void RemoveByPattern(string pattern)
{
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
var keysToRemove = new List<String>(); foreach (var item in Cache)
if (regex.IsMatch(item.Key))
keysToRemove.Add(item.Key); foreach (string key in keysToRemove)
{
Remove(key);
}
} /// <summary>
/// Clear all cache data
/// </summary>
public void Clear()
{
foreach (var item in Cache)
Remove(item.Key);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web; namespace Nop.Core.Caching
{
/// <summary>
/// Represents a NopStaticCache
/// </summary>
public partial class PerRequestCacheManager : ICacheManager
{
private readonly HttpContextBase _context; /// <summary>
/// Ctor
/// </summary>
/// <param name="context">Context</param>
public PerRequestCacheManager(HttpContextBase context)
{
this._context = context;
} /// <summary>
/// Creates a new instance of the NopRequestCache class
/// </summary>
protected IDictionary GetItems()
{
if (_context != null)
return _context.Items; return null;
} /// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
public T Get<T>(string key)
{
var items = GetItems();
if (items == null)
return default(T); return (T)items[key];
} /// <summary>
/// Adds the specified key and object to the cache.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
public void Set(string key, object data, int cacheTime)
{
var items = GetItems();
if (items == null)
return; if (data != null)
{
if (items.Contains(key))
items[key] = data;
else
items.Add(key, data);
}
} /// <summary>
/// Gets a value indicating whether the value associated with the specified key is cached
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
public bool IsSet(string key)
{
var items = GetItems();
if (items == null)
return false; return (items[key] != null);
} /// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
public void Remove(string key)
{
var items = GetItems();
if (items == null)
return; items.Remove(key);
} /// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
public void RemoveByPattern(string pattern)
{
var items = GetItems();
if (items == null)
return; var enumerator = items.GetEnumerator();
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
var keysToRemove = new List<String>();
while (enumerator.MoveNext())
{
if (regex.IsMatch(enumerator.Key.ToString()))
{
keysToRemove.Add(enumerator.Key.ToString());
}
} foreach (string key in keysToRemove)
{
items.Remove(key);
}
} /// <summary>
/// Clear all cache data
/// </summary>
public void Clear()
{
var items = GetItems();
if (items == null)
return; var enumerator = items.GetEnumerator();
var keysToRemove = new List<String>();
while (enumerator.MoveNext())
{
keysToRemove.Add(enumerator.Key.ToString());
} foreach (string key in keysToRemove)
{
items.Remove(key);
}
}
}
}
using System; namespace Nop.Core.Caching
{
/// <summary>
/// Extensions
/// </summary>
public static class CacheExtensions
{
public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire)
{
return Get(cacheManager, key, , acquire);
} public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, Func<T> acquire)
{
if (cacheManager.IsSet(key))
{
return cacheManager.Get<T>(key);
}
else
{
var result = acquire();
//if (result != null)
cacheManager.Set(key, result, cacheTime);
return result;
}
}
}
}
二、插件
http://www.cnblogs.com/haoxinyue/archive/2013/06/06/3105541.html
nopCommerce_3.00-Nop.Core.Caching的更多相关文章
- ASP.NET Core Caching简介
在.NET Core中提供了Caching的组件.目前Caching组件提供了三种存储方式: Memory Redis SQLSever 1.Memeor Caching 新建一个ASP.NET Co ...
- Nop中的Cache浅析
Nop中定义了ICacheManger接口,它有几个实现,其中MemoryCacheManager是内存缓存的一个实现. MemoryCacheManager: using System; using ...
- [转]nopCommerce Widgets and How to Create One
本文转自:https://dzone.com/articles/what-are-nopcommerce-widgets-and-how-to-create-one A widget is a sta ...
- Nopcommerce 二次开发2 WEB
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel.Syndica ...
- [转]教你一招 - 如何给nopcommerce增加新闻类别模块
本文转自:http://www.nopchina.net/post/nopchina-teach-newscategory.html nopcommerce的新闻模块一直都没有新闻类别,但是很多情况下 ...
- nopCommerce 数据缓存
为了提高一个系统或网站的性能和IO吞吐量,我们一般都会采用缓存技术.当然NopCommerce也不例外,本文我们就来给大家分析一下nop中Cache缓存相关类设计.核心源码及实现原理. 一.Nop.C ...
- nopCommerce 3.9 大波浪系列 之 使用Redis主从高可用缓存
一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集群. 一般来说,只有一台Redis是不可行的,原因如下: 单台Redis服务器会发 ...
- nopcommerce 4.1 学习2 -插件之挂件
先了解下nop4.1的一些插件有哪些类型: 1.支付插件 Nop.Plugin.Payments.PayPalStandard Nop.Plugin.Payments.CheckMoneyOrd ...
- SmartStore.Net、NopCommerce 全局异常处理、依赖注入、代码研究
以下是本人最近对NopCommerce和SmartStore.net部分代码的研究和总结,主要集中于:依赖注入.异常处理.对象映射.系统缓存.日志这些方面,供大家参考. NOP 3.8 /// < ...
随机推荐
- time.h文件中包含的几个函数使用时须注意事项
time.h头文件中包含以下函数 char* asctime(const struct tm *tm); char* asctime_r(const struct tm *tm,char *buf); ...
- mysql远程连接缓慢的问题
这两天发现服务器程序启动的时候到了mysql初始连接的那一步很耗时,启动缓慢,后来发现,将连接的主机的-h参数改成localhost的时候 瞬间就完成连接了.后来在网上查到,原来是由于mysql服务器 ...
- ubuntu server 安装
http://tigerlchen.iteye.com/blog/1765765 解决CDROM找不到的bug
- jquery右下角返回顶部
实现的效果也就是,当有滚动条是,滚动条未动或与顶部距离小于多少像素是,返回顶部按钮处于隐身状态,当滚动条与顶部距离大于一定像素时,返回顶部按钮出现,实现点击‘返回按钮’后,从当前位置回到等不位置.要先 ...
- 搭建mysql主从复制---Mysql Replication
主从复制原理 Mysql的Replication是一个异步的复制过程,从一个Mysql Instance(master)复制到另一个Mysql Instance(slave).中间需要三个线程slav ...
- [About me] 关于Alima博主
大家好,欢迎来到我的博客,我是博主Alima. 关于我,一个从岛国工作刚刚失望回国的90后男孩子,被日企伤的很难过. 迫切的想改变现在的一切,想换个城市换个工作方向,重新开始. 如果你,觉得我的博客写 ...
- 开源CMS赏析
国内CMS产品有很多,开源的产品也不少,大概的可分为以下几类: .NET开源产品:We7CMS: PHP开源产品:Dede CMS.PHP CMS和帝国CMS: ASP开源产品:动易SiteFacto ...
- POJ 3264 Balanced Lineup 简单RMQ
题目:http://poj.org/problem?id=3264 给定一段区间,求其中最大值与最小值的差. #include <stdio.h> #include <algorit ...
- iOS Xcode制作模板类-b
为什么要定义模板类 遵守代码规范可以提高代码可读性, 降低后期维护成本. 当我们定下了一个团队都认同的代码规范, 如我们要求所有的viewController的代码都得按照下面来组织: #pragma ...
- Android源码中的FLAG为何使用16进制
1.在阅读源码的时候经常发现有一些标志属性使用一些位操作来判断是否具有该标志,增加标志或者去除标志. 比如View.java中的 /** * This view does not want keyst ...