System.Web.Caching.Cache 方法汇总
在做后台的时候,想着把所有栏目放到缓存里,就这里了一个类。必然是有缺陷,暂时没有实现滑动缓存
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching; namespace WebCore.UI.Common
{
public class Cache
{
private static System.Web.Caching.Cache _cache;
public const int DayFactor = 0x4380;
private static int Factor = ;
public const int HourFactor = ;
public const int MinuteFactor = ;
public const double SecondFactor = 0.2;
static Cache()
{
HttpContext current = HttpContext.Current;
if (current != null)
{
_cache = current.Cache;
}
else
{
_cache = HttpRuntime.Cache;
}
} private Cache()
{
}
public static void Clear()
{
IDictionaryEnumerator enumerator = _cache.GetEnumerator();
ArrayList list = new ArrayList();
while (enumerator.MoveNext())
{
list.Add(enumerator.Key);
}
foreach (string str in list)
{
_cache.Remove(str);
}
} public static object Get(string key)
{
return _cache[key];
} public static void Insert(string key, object obj)
{
Insert(key, obj, null, );
} public static void Insert(string key, object obj, int seconds)
{
Insert(key, obj, null, seconds);
} public static void Insert(string key, object obj, CacheDependency dep)
{
Insert(key, obj, dep, 0x21c0);
} public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
{
Insert(key, obj, null, seconds, priority);
} public static void Insert(string key, object obj, CacheDependency dep, int seconds)
{
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
} public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
{
if (obj != null)
{
_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double)(Factor * seconds)), TimeSpan.Zero, priority, null);
}
} public static void Max(string key, object obj, CacheDependency dep)
{
if (obj != null)
{
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
}
} public static void MicroInsert(string key, object obj, int secondFactor)
{
if (obj != null)
{
_cache.Insert(key, obj, null, DateTime.Now.AddSeconds((double)(Factor * secondFactor)), TimeSpan.Zero);
}
} public static void Remove(string key)
{
_cache.Remove(key);
} public static void RemoveByPattern(string pattern)
{
IDictionaryEnumerator enumerator = _cache.GetEnumerator();
Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
while (enumerator.MoveNext())
{
if (regex.IsMatch(enumerator.Key.ToString()))
{
_cache.Remove(enumerator.Key.ToString());
}
}
} public static void ReSetFactor(int cacheFactor)
{
Factor = cacheFactor;
} public static int SecondFactorCalculate(int seconds)
{
return Convert.ToInt32(Math.Round((double)(seconds * 0.2)));
}
}
}
使用方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching; namespace WebCore.UI.Common
{ public class SetSysMenuCache
{
private const string SiteSettingsCacheKey = "FileCache-SysLeftMenus"; public string ReadCache(bool cacheable,string Uid,Func<string> GetData)
{
string str = "";
str = Cache.Get(Uid + "-" + SiteSettingsCacheKey) as string;
if (str == null || str == "")
{
//缓存不存在
if (cacheable)
{
if (GetData!=null)
{
str=GetData();
}
}
} return str;
}
public void CreatCache(string Datas,string Uid)
{
Cache.Insert(Uid + "-" + SiteSettingsCacheKey, Datas,);
}
public void UpdateCache(string Datas, string Uid)
{
Cache.Remove(Uid + "-" + SiteSettingsCacheKey);
CreatCache(Datas, Uid);
}
}
}
System.Web.Caching.Cache 方法汇总的更多相关文章
- System.Web.Caching.Cache类 缓存 各种缓存依赖
原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...
- 第一节:从程序集的角度分析System.Web.Caching.Cache ,并完成基本封装。
一. 揭开迷雾 1. 程序集准备 a. 需要给项目添加 System.Web 程序集. b. 需要给使用的地方添加两个引用. 2. 程序集探究 在对应的类中输入关键字 Cache,选中点 ...
- System.Web.Caching.Cache类 缓存 各种缓存依赖(转)
转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...
- System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖
Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 一.属性 属性 说明 Count 获取存储在缓存中的 ...
- C# System.Web.Caching.Cache类 缓存 各种缓存依赖
原文:https://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntim ...
- System.Web.Caching.Cache类 缓存
1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...
- 清除 System.Web.Caching.Cache 以"xxx"开头的缓存
public static void ClearStartCache(string keyStart) { List<string> cacheKeys = new List<str ...
- 缓存-System.Web.Caching.Cache
实现 Web 应用程序的缓存. 每个应用程序域创建一个此类的实例,只要应用程序域将保持活动状态,保持有效. 有关此类的实例的信息,请通过Cache的属性HttpContext对象或Cache属性的Pa ...
- System.Web.Caching.Cache缓存帮助类
/// <summary> /// 缓存帮助类 /// </summary> public class CacheHelper { /// <summary> // ...
随机推荐
- Javascript 浏览器探测
1.根据浏览器的性能 var is = { ff: window.globalStorage, ie: document.all && !window.opera, ie6: !win ...
- postgresql 抽样查询
curl -GET 'http://****/query' --data-urlencode "db=db" --data-urlencode "q=SELECT las ...
- Ubuntu“无法解析或打开软件包的列表或是状态文件”的解决办法。_StarSasumi_新浪博客
Ubuntu"无法解析或打开软件包的列表或是状态文件"的解决办法. (2011-04-30 14:56:14) 转载▼ 标签: ubuntu apt 分类: Ubuntu/Linu ...
- shell字符串操作之cut---实现字符串截取
shell中(字符串截取) cut是以每一行为一个处理对象的,这种机制和sed是一样的.(关于sed的入门文章将在近期发布) 2 cut一般以什么为依据呢? 也就是说,我怎么告诉cut我想定位到的剪切 ...
- EHCache分布式缓存集群环境配置
EHCache分布式缓存集群环境配置 ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms.同时ehcache可以可以实现多播的方式实现集群,也可以手动指定集群主机序列实现集群 ...
- STM8S awu及看门狗IWDG WWDG应用(转)
源:STM8S awu及看门狗IWDG WWDG应用 AWU的应用(用库函数完成的) //切记要开启中断 且在中断函数中 AWU_GetFlagStatus(); 来清除中断 void AWU_SET ...
- angular中的$http配置和参数
依赖:$httpBackend $cacheFactory $rootScope $q $injector 使用:$http(config); 参数: method:字符串,请求方法. url:字符串 ...
- 《算法导论》习题2.3-6 改进的InsertSort
InsertSort中有关键的一步是把当前元素A[i]插入到已经排好序的A[1,i-1]的合适的位置上,在原始的InsertSort算法中, 采用的是从后往前一步一步查找的方法,习题2.3-6要求利用 ...
- 360路由器设置网段ip
路由器设置->高级设置->修改路由器地址
- EF的join用法
var customers = DB.Customer.Join(DB.Commission, cst => cst.CommissionId, ...