• 缓存的使用


    关于缓存的方法即用法介绍,命名空间:System.Web.Caching。

    第一、  Cache中的Add方法:

    参数:

    Cache.Add(

    string key,//Cache中的键

    Object value,//Cache中的值
    CacheDependency dependencies,//依赖的项,可以是某个文件路径无则为null
    DateTime absoluteExpiration,//绝对过期的时间
    TimeSpan slidingExpiration,//滑动过期的时间
    CacheItemPriority priority,//缓存删除的优先级
    CacheItemRemovedCallback onRemoveCallback//移除调用的回调函数对象时调用的委托(如果有)无则为null

    实例:
    if (Cache["Key1"] == null)
    {
        Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, null);
    }


    第二、Cache中的Get方法:返回的是一个对象Object

    Cache.Get("Key1");

    第三、Cache中的GetEnumerator方法:检索用于循环访问包含在缓存中的键设置及其值的字典枚举数。

    IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
    while (CacheEnum.MoveNext())
    {
      cacheItem = Server.HtmlEncode(CacheEnum.Current.ToString());
      Response.Write(cacheItem);
    }

    第四、Cache中Insert方法

     Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero, CacheItemPriority.High, onRemove);

     备注:

    无法同时设置 absoluteExpiration 和 slidingExpiration 参数。如果要让缓存项在特定时间过期,可将 absoluteExpiration 参数设置为特定时间,并将 slidingExpiration 参数设置为 NoSlidingExpiration。

     如果要让缓存项自最后一次访问该项后的某段时间之后过期,可将 slidingExpiration 参数设置为过期间隔,并将 absoluteExpiration 参数设置为 NoAbsoluteExpiration

     第五、Cache中的Remove方法:未找到则为null即空引用

    if(Cache["Key1"] != null)
    {
      Cache.Remove("Key1");
    }

  • 相关阅读:
    浅谈工作流的作用
    WinForm上播放Flash文件
    C#反序列化 “在分析完成之前就遇到流结尾”
    UML类图详解
    关于C#中Thread.Join()的一点理解
    c# 反射的用法
    C#多线程学习笔记之(abort与join配合使用)
    UML用例图总结
    asp.net 发布到IIS中出现”处理程序“PageHandlerFactoryIntegrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”“错误的解决方法
    C#序列化和反序列化
  • 原文地址:https://www.cnblogs.com/professional-NET/p/4805961.html
Copyright © 2020-2023  润新知