164 lines
4.8 KiB
C#
164 lines
4.8 KiB
C#
using System;
|
|
using System.Web;
|
|
|
|
namespace WX.CRM.Common
|
|
{
|
|
public class CacheHelper
|
|
{
|
|
#region 判断Cache存在
|
|
/// <summary>
|
|
/// 判断cahce是否存在
|
|
/// </summary>
|
|
/// <param name="key">key</param>
|
|
/// <returns></returns>
|
|
public static bool Exists(string key)
|
|
{
|
|
bool result = false;
|
|
if (HttpRuntime.Cache[key] != null)
|
|
{
|
|
result = true;
|
|
}
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
#region 获取Cache
|
|
/// <summary>
|
|
/// 获取缓存
|
|
/// </summary>
|
|
/// <typeparam name="T">类型</typeparam>
|
|
/// <param name="key">Key</param>
|
|
/// <returns></returns>
|
|
public static T Get<T>(string key)
|
|
{
|
|
try
|
|
{
|
|
T t = (T)HttpRuntime.Cache[key];
|
|
return t;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 尝试获取某个缓存
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="key"></param>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static bool TryGet<T>(string key, ref T data)
|
|
{
|
|
try
|
|
{
|
|
data = (T)HttpRuntime.Cache[key];
|
|
return data != null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 设置Cache
|
|
/// <summary>
|
|
/// 设置缓存
|
|
/// </summary>
|
|
/// <typeparam name="T">类型</typeparam>
|
|
/// <param name="key">key</param>
|
|
/// <param name="t">value</param>
|
|
public static void Set<T>(string key, T t)
|
|
{
|
|
HttpRuntime.Cache.Insert(key, t);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置缓存
|
|
/// </summary>
|
|
/// <typeparam name="T">类型</typeparam>
|
|
/// <param name="key">key</param>
|
|
/// <param name="t">value</param>
|
|
/// <param name="expiresTime">过期时间</param>
|
|
/// <param name="isXD">是否为相对过期时间</param>
|
|
public static void Set<T>(string key, T t, DateTime expiresTime, bool isXD = true)
|
|
{
|
|
if (isXD)
|
|
HttpRuntime.Cache.Insert(key, t, null, expiresTime, System.Web.Caching.Cache.NoSlidingExpiration);
|
|
else
|
|
HttpRuntime.Cache.Insert(key, t, null, expiresTime, TimeSpan.Zero);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 文件组依赖缓存
|
|
/// </summary>
|
|
/// <typeparam name="T">类型</typeparam>
|
|
/// <param name="key">key</param>
|
|
/// <param name="t">value</param>
|
|
/// <param name="expiresTime">过期时间</param>
|
|
/// <param name="filePath">文件组</param>
|
|
public static void Set<T>(string key, T t, DateTime expiresTime, string[] filePaths)
|
|
{
|
|
HttpRuntime.Cache.Insert(key, t, new System.Web.Caching.CacheDependency(filePaths), expiresTime, TimeSpan.Zero);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 文件依赖缓存
|
|
/// </summary>
|
|
/// <typeparam name="T">类型</typeparam>
|
|
/// <param name="key">key</param>
|
|
/// <param name="t">value</param>
|
|
/// <param name="expiresTime">过期时间</param>
|
|
/// <param name="filePath">文件</param>
|
|
public static void Set<T>(string key, T t, DateTime expiresTime, string filePath)
|
|
{
|
|
HttpRuntime.Cache.Insert(key, t, new System.Web.Caching.CacheDependency(filePath), expiresTime, TimeSpan.Zero);
|
|
}
|
|
#endregion
|
|
|
|
#region 删除缓存
|
|
public static void Remove(string key)
|
|
{
|
|
if (Exists(key))
|
|
{
|
|
HttpRuntime.Cache.Remove(key);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 缓存处理
|
|
static CacheHelper lockobj = new CacheHelper();
|
|
/// <summary>
|
|
/// 检查缓存是否存在
|
|
///
|
|
/// </summary>
|
|
/// <param name="userName"></param>
|
|
/// <returns></returns>
|
|
public static bool CheckTheCacheAndSava(string userName)
|
|
{
|
|
bool bExists = false;
|
|
lock (lockobj)
|
|
{
|
|
bExists = HttpRuntime.Cache[userName] != null;
|
|
if (!bExists)
|
|
{
|
|
HttpRuntime.Cache.Add(userName, "1", null, DateTime.Now.AddSeconds(120), System.Web.Caching.Cache.NoSlidingExpiration,
|
|
System.Web.Caching.CacheItemPriority.High, null);
|
|
}
|
|
}
|
|
return bExists;
|
|
}
|
|
|
|
|
|
|
|
public static void RemoveRegCache(string userName)
|
|
{
|
|
|
|
CacheHelper.Remove(userName);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|