System.Runtime.Caching命名空间是.NET 4.0新增的,目的是将以前的.NET 版本中的System.Web.Caching单独提取出来,独立使用,这样web和其他.NET程序如WPF都可以使用。

System.Runtime.Caching包含缓存类和监视类,包括文件、数据库、缓存等的监视,与以前在System.Web.Caching中的一样,但重新包装。

可以预见在以后的版本中,System.Web.Caching命名空间会被标记为Obsolete(过时),或者重写为包装System.Runtime.Caching中的方法。

using System.Runtime.Caching;
public static string GetToken()
{
ObjectCache oCache = MemoryCache.Default;
string fileContents = oCache["wechart_token"] as string;
if (fileContents == null)
{
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now.AddMinutes();//取得或设定值,这个值会指定是否应该在指定期间过后清除
fileContents = //这里赋值;
oCache.Set("wechart_token", fileContents, policy);
}
return fileContents;
}

C# System.Runtime.Caching使用的更多相关文章

  1. HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  2. System.Runtime.Caching中MemoryCache帮助类

    值得参考的几个内存缓存帮助类: 参考资料: https://github.com/Hendy/memory-cache-helper https://gist.github.com/jdalley/0 ...

  3. VS2015 出现 .NETSystem.Runtime.Remoting.RemotingException: TCP 错误

    错误内容: 界面显示内容为: .NET�������������System.Runtime.Remoting.RemotingException: TCP 淇¢亾鍗忚鍐茬獊: 搴斾负鎶ュご銆� 鍦 ...

  4. 找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”的解决办法

    找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”.的解决办法站点发布后部署到了两 ...

  5. Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute'

    [TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from as ...

  6. 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则

    在.NET 4.0下使用最新版本的log4Net 1.2.10,会遇到下面这样的错误: 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObject ...

  7. IIS提示“异常详细信息: System.Runtime.InteropServices.ExternalException: 无法执行程序”

    先来看错误提示: 无法执行程序.所执行的命令为 "C:/Windows/Microsoft.NET/Framework/v3.5/csc.exe" /noconfig /fullp ...

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

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

  9. System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800AC472

    更新至服务器后运行出错: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800AC472 解决方法 注册 ...

随机推荐

  1. Python3小知识点

    1. 是转义字符,\可以输出.在一行的末尾单独的一个\表示这行没有结束,下一行接着写 2.可以用"'或"""把一大段话引起来(可以换行)然后赋值,输出. 3.要 ...

  2. swust oj 1012

    哈希表(链地址法处理冲突) 1000(ms) 10000(kb) 2542 / 6517 采用除留余数法(H(key)=key %n)建立长度为n的哈希表,处理冲突用链地址法.建立链表的时候采用尾插法 ...

  3. 【转】priority_queue优先队列

    转自:http://www.cppblog.com/shyli/archive/2007/04/06/21366.html http://www.cppblog.com/shyli/archive/2 ...

  4. OpenCV读写摄像头并写入视频

    #include <opencv2/opencv.hpp>using namespace cv;#include <iostream>using namespace std; ...

  5. python Ajax

    Ajax一.准备知识JSON1.什么是json JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSO ...

  6. JNI实战(二):Java 调用 C

    1. JNI Env 和 Java VM 关系说明 JNIEnv 是 Java的本地化环境,是Java与C的交互的重要桥梁. 在Android上,一个进程对应一个JavaVM,也就是一个app对应一个 ...

  7. http请求抓包神器-Fiddler(记录和检查你电脑的所有http通讯)

    Fiddler是做什么的,能帮助我们做什么? 1.能够监听http/httpS的流量,可以截获从浏览器或者客户端软件向服务器发送的http/https请求: 2.对截获之后的请求,我们还能够查看请求中 ...

  8. 吴恩达机器学习笔记52-异常检测的问题动机与高斯分布(Problem Motivation of Anomaly Detection& Gaussian Distribution)

    一.问题动机 异常检测(Anomaly detection)问题是机器学习算法的一个常见应用.这种算法的一个有趣之处在于:它虽然主要用于非监督学习问题,但从某些角度看,它又类似于一些监督学习问题. 给 ...

  9. [Swift]LeetCode958. 二叉树的完全性检验 | Check Completeness of a Binary Tree

    Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...

  10. Mac下安装配置Python2和Python3并相互切换使用

    mac os 以前没有使用过,这次使用了一把,的确还是比较不顺手的,估计从今以后,就要把平台逐渐切换到mac了.今后好的文章,专门会开一个macos专栏,专门记录macos的使用过程中的心得,体会,以 ...