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. 对象池----unity中应用

    对象池应用在unity中能减少资源消耗,节省内存空间具体原理不再赘述. 以下是他的操作步骤:(注意:对象池中应用到了栈或对队列!) 1).先建立一个(怪物)物体   mMonster; 2).再建立一 ...

  2. Java中不定项参数(可变参数)的使用

    Java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理. 注意事项:   1)不定项参数必须放在参数列表最后一个.   2)不定项参数只能有一个(多 ...

  3. Mesos源码分析(8): Mesos-Slave的初始化

      Mesos-Slave的初始化在文件src/slave/slave.cpp里面     首先初始化资源预估器   初始化attributes   初始化hostname     初始化status ...

  4. [Swift]LeetCode910. 最小差值 II | Smallest Range II

    Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...

  5. [Swift]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

    In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...

  6. Redis 设计与实现 (九)--Lua

    EVAL script numkeys key [key ...] arg [arg ...] script:     你的lua脚本 numkeys:  key的个数 key:           ...

  7. Go语言的一些使用心得

    序 起初一直使用的Python,到了18年下半年由于业务需求而接触了Golang,从开始学习到现在的快半年里,也用Golang写了些代码,公司产品和业余写的都有,今天就写点Golang相关的总结或者感 ...

  8. python判断文件是否存在

    # 判断文件是否存在 def judgejson(jsonpath): # 如果存在就返回True,不存在就返回False return os.path.exists(jsonpath)

  9. Python内置函数(61)——str

    英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...

  10. WeakHashMap

    WeakHashMap 今天在具体业务的时候看到HashMap和WeakHashMap的区别.因为PHP语言并没有这种概念.所以很好奇做了一下研究. WeakHashMap WeakHashMap所谓 ...