Files
lserp_cs_5.0/插件库/Lskj.DevFx/Cache/CacheHelper.cs
T
2026-07-10 15:25:05 +08:00

159 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/******************************************************************************
Copyright 2005-2007 R2@DevFx.NET
DevFx.NET is free software; you can redistribute it and/or modify
it under the terms of the Lesser GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
DevFx.NET is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Lesser GNU General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/*******************************************************************************/
using Lskj.DevFx.Cache.Config;
using Lskj.DevFx.Config;
namespace Lskj.DevFx.Cache
{
/// <summary>
/// 缓存实用方法类
/// </summary>
public static class CacheHelper
{
#region constructor
static CacheHelper() {
//创建缓存管理器
CreateCacheManager();
}
#endregion
#region private static members
private static ICacheManager cacheManager;
private static void CreateCacheManager() {
cacheManager = new CacheManager();
cacheManager.Init(null);
}
#endregion
#region public static members
/// <summary>
/// 获取或创建缓存
/// </summary>
/// <param name="name">缓存名称</param>
/// <param name="interval">清除过期缓存项的时间周期,单位:毫秒</param>
/// <returns></returns>
public static ICache GetOrCreateCache(string name)
{
return GetOrCreateCache(name, 0);
}
/// <summary>
/// 获取或创建缓存
/// </summary>
/// <param name="name">缓存名称</param>
/// <param name="interval">清除过期缓存项的时间周期,单位:毫秒</param>
/// <returns></returns>
public static ICache GetOrCreateCache(string name,int interval)
{
ICache cacher = CacheHelper.GetCache(name);
if (cacher == null)
return CacheHelper.CreateCache(name, interval);
else
return cacher;
}
/// <summary>
/// 获取已配置的缓存器
/// </summary>
/// <param name="cacheName">配置文件中配置的缓存器名称</param>
/// <returns>ICache的实例</returns>
public static ICache GetCache(string cacheName) {
if(cacheManager != null) {
return cacheManager.GetCache(cacheName);
} else {
return null;
}
}
/// <summary>
/// 创建一个指定名称的缓存空间
/// </summary>
/// <param name="cacheName">缓存名称</param>
/// <returns>ICache的实例</returns>
public static ICache CreateCache(string cacheName)
{
return CreateCache(cacheName,0);
}
/// <summary>
/// 创建一个指定名称的缓存空间
/// </summary>
/// <param name="cacheName">缓存名称</param>
/// <param name="interval">清除过期缓存项的时间周期,单位:毫秒</param>
/// <returns>ICache的实例</returns>
public static ICache CreateCache(string cacheName,int interval)
{
if (cacheManager != null)
{
return cacheManager.AddCache(cacheName,interval);
}
else
{
return null;
}
}
/// <summary>
/// 获取缓存项值
/// </summary>
/// <param name="cacheName">配置文件中配置的缓存器名称</param>
/// <param name="key">缓存项健值</param>
/// <returns>缓存项值,如果没有命中,则返回<c>null</c></returns>
public static object GetCacheValue(string cacheName, string key) {
return GetCacheValue(cacheName, key, false);
}
/// <summary>
/// 获取缓存项值
/// </summary>
/// <param name="cacheName">配置文件中配置的缓存器名称</param>
/// <param name="key">缓存项健值</param>
/// <param name="throwOnError">如果有错误,是否抛出异常</param>
/// <returns>缓存项值,如果没有命中,则返回<c>null</c></returns>
public static object GetCacheValue(string cacheName, string key, bool throwOnError) {
ICache cache = GetCache(cacheName);
object @value = null;
if(cache == null) {
if(throwOnError) {
throw new CacheException("没有配置Cache" + cacheName);
}
} else {
@value = cache[key];
}
return @value;
}
/// <summary>
/// 移除所有缓存空间
/// </summary>
public static void Clear()
{
if (cacheManager != null)
cacheManager.Clear();
}
#endregion
}
}