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

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. openssl windows编译 32位&64位

    openssl版本:openssl-1.0.0k 64位编译 1.编译环境: openssl-1.0.0a必须用vs2008编译(Open Visual Studio 2008 x64 Cross T ...

  2. DataTable的名称要后设置

    string sqldatabase = string.Format(dr["sql"].ToString(), drpat["PATIENT_ID"].ToS ...

  3. 计算机学院大学生程序设计竞赛(2015’12) 1002 Polygon

    #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #inclu ...

  4. Memcached缓存系统介绍及安装

    1.什么是Memcached 1.1.Memcached概述 Memcached是一个免费的开源的.高性能的.具有又分布式内存对象的缓存系统,它通过减轻数据库负载加速动态WEB应用, 1.2.Memc ...

  5. HTML5 Canvas | w3cschool菜鸟教程

    HTML5 Canvas <canvas> 标签定义图形,比如图表和其他图像,您必须使用脚本来绘制图形.. 在画布上(Canvas)画一个红色矩形,梯度矩形,彩色矩形,和一些彩色的文字. ...

  6. C++异常(exception)第三篇-------如何释放资源(没有finally语句)

    问题: C++中没有finally,那么应该在哪里关闭资源? C++的try{}catch(){},为什么没有finally{}块来释放资源? 如类中的一个方法,局部变量 File * file=NU ...

  7. 无法访问 ASP 兼容性模式

    <%@ Page Title="" Language="C#" MasterPageFile="../theme/classic/content ...

  8. 23、手把手教你Extjs5(二十三)模块Form的自定义的设计[2]

    在本节中将要加入各种类型的字段,在加入字段的时候由于可以一行加入多个字段,因此层次结构又多了一层fieldcontainer.form里面的主要层次结构如下: form -- fieldSet -- ...

  9. UIP源码之ARP过程分析

    之前我们使用UIP实现了tcp和udp通讯今天来说说UIP的实现流程,当然,这篇文章里面只会涉及tcp和udp,暂时还没办法说DHCP,因为UIP的DHCP实现使用了协程的概念,下一章将协程之后再说D ...

  10. Android SQLite 加密模块实现入门

    安卓的安全性那是众所周知,最近学习安卓apk反编译,发现某些即时通讯软件都封装了自己独立使用的数据库模块(从framework java/C++ 一直到底层的SQLite的C库), 为了防止被root ...