150 lines
5.2 KiB
C#
150 lines
5.2 KiB
C#
using Services.Enums;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Web;
|
||
using System.Web.Mvc;
|
||
|
||
namespace Services.Extensions
|
||
{
|
||
/// <summary>
|
||
/// 缓存扩展
|
||
/// </summary>
|
||
public static class CacheExtensions
|
||
{
|
||
public static MvcHtmlString Cache(this HtmlHelper htmlHelper, string cacheName, Func<object> func, CacheTimeType cacheTimeType, int cacheTime)
|
||
{
|
||
if (!CacheExtensions.CheckCache(cacheName))
|
||
{
|
||
CacheExtensions.SetCache(cacheName, func().ToString(), cacheTimeType, cacheTime);
|
||
}
|
||
return MvcHtmlString.Create(CacheExtensions.GetCache<string>(cacheName));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查缓存名是否存在,如果存在则返回True
|
||
/// </summary>
|
||
/// <param name="cacheName">缓存的枚举类</param>
|
||
public static bool CheckCache(string cacheName)
|
||
{
|
||
return HttpRuntime.Cache[cacheName.ToString()] != null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取缓存,未做校验,每次取缓存的时候需要判断缓存是否存在
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="cacheName"></param>
|
||
/// <param name="varName"></param>
|
||
/// <returns></returns>
|
||
public static T GetCache<T>(string cacheName)
|
||
{
|
||
return (T)((object)HttpRuntime.Cache[cacheName]);
|
||
}
|
||
|
||
public static T GetCacheWithSet<T>(string cacheName, Func<T> valueFunc, CacheTimeType cacheTimeType, int times)
|
||
{
|
||
if (!CacheExtensions.CheckCache(cacheName))
|
||
{
|
||
CacheExtensions.SetCache(cacheName, valueFunc(), cacheTimeType, times);
|
||
}
|
||
return CacheExtensions.GetCache<T>(cacheName);
|
||
}
|
||
|
||
public static T GetCacheWithSet<T>(string cacheName, Func<T> valueFunc)
|
||
{
|
||
if (!CacheExtensions.CheckCache(cacheName))
|
||
{
|
||
CacheExtensions.SetCache(cacheName, valueFunc());
|
||
}
|
||
return CacheExtensions.GetCache<T>(cacheName);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取缓存集合,未做校验,每次取缓存的时候需要判断缓存是否存在
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="cacheName"></param>
|
||
/// <param name="varName"></param>
|
||
/// <returns></returns>
|
||
public static List<T> GetCacheList<T>(string cacheName)
|
||
{
|
||
return (List<T>)HttpRuntime.Cache[cacheName];
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置缓存
|
||
/// </summary>
|
||
/// <param name="cacheName"></param>
|
||
/// <param name="value"></param>
|
||
/// <param name="cacheTimeType"></param>
|
||
/// <param name="cacheTime"></param>
|
||
public static void SetCache(string cacheName, object value, CacheTimeType cacheTimeType, int cacheTime)
|
||
{
|
||
switch (cacheTimeType)
|
||
{
|
||
case CacheTimeType.ByMinutes:
|
||
HttpRuntime.Cache.Insert(cacheName, value, null, DateTime.Now.AddMinutes((double)cacheTime), TimeSpan.Zero);
|
||
return;
|
||
case CacheTimeType.ByHours:
|
||
HttpRuntime.Cache.Insert(cacheName, value, null, DateTime.Now.AddHours((double)cacheTime), TimeSpan.Zero);
|
||
return;
|
||
case CacheTimeType.ByDays:
|
||
HttpRuntime.Cache.Insert(cacheName, value, null, DateTime.Now.AddDays((double)cacheTime), TimeSpan.Zero);
|
||
return;
|
||
case CacheTimeType.ByYears:
|
||
HttpRuntime.Cache.Insert(cacheName, value, null, DateTime.Now.AddYears(cacheTime), TimeSpan.Zero);
|
||
return;
|
||
default:
|
||
return;
|
||
}
|
||
}
|
||
|
||
public static void SetCache(string cacheName, object value)
|
||
{
|
||
HttpRuntime.Cache.Insert(cacheName, value);
|
||
}
|
||
|
||
public static List<string> GetAllCache()
|
||
{
|
||
List<string> list = new List<string>();
|
||
IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
|
||
while (enumerator.MoveNext())
|
||
{
|
||
list.Add(enumerator.Key.ToString());
|
||
}
|
||
return list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清除指定缓存
|
||
/// </summary>
|
||
/// <param name="cacheName"></param>
|
||
public static void ClearCache(string cacheName)
|
||
{
|
||
if (CacheExtensions.CheckCache(cacheName))
|
||
{
|
||
HttpRuntime.Cache.Remove(cacheName);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重置缓存
|
||
/// </summary>
|
||
/// <param name="cacheName"></param>
|
||
/// <param name="value"></param>
|
||
public static void RestCache(string cacheName, object value)
|
||
{
|
||
if (CacheExtensions.CheckCache(cacheName))
|
||
{
|
||
HttpRuntime.Cache[cacheName] = value;
|
||
return;
|
||
}
|
||
CacheExtensions.SetCache(cacheName, value);
|
||
}
|
||
}
|
||
}
|