在做后台的时候,想着把所有栏目放到缓存里,就这里了一个类。必然是有缺陷,暂时没有实现滑动缓存

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 方法汇总的更多相关文章

  1. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  2. 第一节:从程序集的角度分析System.Web.Caching.Cache ,并完成基本封装。

    一. 揭开迷雾 1. 程序集准备 a.  需要给项目添加 System.Web 程序集. b.  需要给使用的地方添加两个引用. 2. 程序集探究      在对应的类中输入关键字 Cache,选中点 ...

  3. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

  4. System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖

    Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 一.属性 属性 说明 Count 获取存储在缓存中的 ...

  5. C# System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:https://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntim ...

  6. System.Web.Caching.Cache类 缓存

    1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...

  7. 清除 System.Web.Caching.Cache 以"xxx"开头的缓存

    public static void ClearStartCache(string keyStart) { List<string> cacheKeys = new List<str ...

  8. 缓存-System.Web.Caching.Cache

    实现 Web 应用程序的缓存. 每个应用程序域创建一个此类的实例,只要应用程序域将保持活动状态,保持有效. 有关此类的实例的信息,请通过Cache的属性HttpContext对象或Cache属性的Pa ...

  9. System.Web.Caching.Cache缓存帮助类

    /// <summary> /// 缓存帮助类 /// </summary> public class CacheHelper { /// <summary> // ...

随机推荐

  1. 计算机学院大学生程序设计竞赛(2015’12) 1009 The Magic Tower

    #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using ...

  2. nginx 内存池分析

    最近nginx的源码刚好研究到内存池,这儿就看下nginx内存池的相关的东西. 一,为什么要使用内存池 大多数的解释不外乎提升程序的处理性能及减小内存中的碎片,对于性能优化这点主要体现在: (1)系统 ...

  3. DOM对象和JQuery对象进行转换

    var btn=document.getElementById("btn"); $(btn).click(function(){}); DOM对象转换为JQuery对象: 用$符号 ...

  4. w3c学习总结1

    1.根据 HTML5 规范,在没有其他合适标签更合适时,才应该把 <b> 标签作为最后的选项.HTML5 规范声明:应该使用 <h1> - <h6> 来表示标题,使 ...

  5. sublime text3 Emmet (原zenCoding)安装方法

    1.安装使用Package Control组件安装 (1)打开控制台 (mac)control+`; (win)ctrl+` (2)复制一下代码并回车 import urllib.request,os ...

  6. tempnam问题

    tempnam()函数创建一个具有唯一文件名的临时文件 若成功,则返回新的临时文件名,若失败,则返回false 失败原因 c:\windows\temp文件夹不具备读写权限(即 不是超级管理员)

  7. hibernate---树状映射

    总公司--分公司1, 分公司2 分公司1: 分公司1下部门1, 分公司1下部门2 分公司2: Org.java: package com.bjsxt.hibernate; import java.ut ...

  8. c循环程序

    6.用双循环打印n行如下图形. * *** ***** ******* 6 7 8 #include<stdio.h> 9 int main() 10 { 11 int i=0,j=0,n ...

  9. Identifying Dialogue Act Type

    Natural Language Processing with Python Chapter  6.2 import nltk from nltk.corpus import nps_chat as ...

  10. (简单) POJ 3368 Frequent values,RMQ。

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...