zoukankan      html  css  js  c++  java
  • Cache在aspx.cs和xxx.CS里的不同用法

    在普通aspx.cs代码里可以用:

    Cache cache = new Cache();

    但在XXXX.CS里,就不能用上面的方式了,得用:

    永不时间过期

    HttpContext.Current.Cache.Insert("Name", "王翔", null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);

    Name:Key

    王翔:Value

    null:表示没有缓存依赖项

    DateTime.MaxValue:时间的最大值(9999-99-99 12:59:59),表示不使用绝对时间过期策略

    TimeSpan.Zero:表示不使用平滑过期

    CacheItemPrority.NotRemovable:表示优先权为不删除该Cache

    null:不怎么用,就null吧

    绝对时间过期 (10秒后,自动过期)

    HttpContext.Current.Cache.Insert("Name", "王翔", null, DateTime.Now.AddSeconds(10), TimeSpan.Zero, CacheItemPriority.NotRemovable, null);

    平滑时间过期 (连续10秒没有访问该缓存,则自动过期)

    HttpContext.Current.Cache.Insert("Name", "王翔", null, DateTime.MaxValue, TimeSpan.FromSeconds(10));

     

    缓存的更新策略

    if (HttpContext.Current.Cache["UserCacheList"] != null)
                {
                    ht = (Hashtable)HttpContext.Current.Cache["UserCacheList"];
                    ht.Add(uId, HttpContext.Current.Cache["User" + uId]);

                }
                else
                {
                    ht.Add(uId, HttpContext.Current.Cache["User" + uId]);
                    //HttpContext.Current.Cache["UserCacheList"] = ht;
                    HttpContext.Current.Cache.Insert("UserCacheList", ht, null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
                }

     

  • 相关阅读:
    Sql server Always On 读写分离配置方法
    MSSQL AlwaysOn中的“主角色中的连接”和“可读辅助副本”
    Windows Server 2019 Active Directory (AD域)时间不同步的解决方法
    windows server 2008 R2 域中用组策略隐藏指定磁盘驱动器(盘符)
    pg数据库优化
    Qlik
    如何查看数据库特别慢
    针对数量的null值最好要做变0处理
    pg如何实现月份的加减
    sql
  • 原文地址:https://www.cnblogs.com/kingfly/p/1674318.html
Copyright © 2011-2022 走看看