zoukankan      html  css  js  c++  java
  • 数据缓存的几种方式

                //绝对时间的缓存设置
                if (HttpRuntime.Cache["time"] == null)
                {
                    object obj = DateTime.Now;
                    //设置绝对过期时间缓存
                    HttpRuntime.Cache.Add(
                        "time",//缓存的键
                        obj,//缓存的值
                        null,//缓存的依赖项
                        DateTime.Now.AddSeconds(10),//过期时间
                        System.Web.Caching.Cache.NoSlidingExpiration,//设置没有相对过期时间
                         System.Web.Caching.CacheItemPriority.Normal,//过期策略
                         null//移除时的回调函数
                        );
                    Response.Write("缓存设置成功,时间为10s,设置缓存的时间为:" + obj.ToString());
                }
                else
                {
                    Response.Write(HttpRuntime.Cache["time"].ToString());
                }
                ///相对过期时间的设置
                if (HttpRuntime.Cache["time"] == null)
                {
                    object obj = DateTime.Now;//将这个时间想象成从数据库中得到的数据
    
                    //设置相对过期时间缓存
                    //  HttpRuntime.Cache["asdf"] = "asfasfd";//永久缓存
                    HttpRuntime.Cache.Add(
                        "time",//
                        obj,//
                        null,//缓存依赖项
                        System.Web.Caching.Cache.NoAbsoluteExpiration,//设置没有绝对过期时间
                       new TimeSpan(0, 0, 10),//相对过期时间
                         System.Web.Caching.CacheItemPriority.Normal,//过期策略
                          null//回调函数
                        );
                    Response.Write("缓存设置成功:" + obj.ToString());
                }
                else
                {
                    Response.Write("缓存的时间为:" + HttpRuntime.Cache["time"].ToString());
                }
                //文件依赖缓存
                if (HttpRuntime.Cache["time"] == null)
                {
                    object obj = DateTime.Now;
                    //创建缓存依赖项
                    System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(Server.MapPath("/数据缓存/1.txt"));
    
                    //创建缓存对象
                    HttpRuntime.Cache.Add(
                        "time",//
                        obj,//
                        dep,//依赖项
                        System.Web.Caching.Cache.NoAbsoluteExpiration,//没有绝对过期时间
                        System.Web.Caching.Cache.NoSlidingExpiration,//没有相对过期时间
                        System.Web.Caching.CacheItemPriority.Normal,//过期策略
                        null//回调函数
                        );
                    Response.Write("文件依赖缓存设置成功:" + obj.ToString());
                }
                else
                {
                    //直接输出
                    Response.Write("缓存的时间为:" + HttpRuntime.Cache["time"].ToString());
                }
               //数据库依赖缓存
                if (HttpRuntime.Cache["time"] == null)
                {
                    object obj = DateTime.Now;
    
    
                    //第一个参数:找到配置文件中配置项的名称,第二个参数:要缓存表的名称
                    System.Web.Caching.SqlCacheDependency sqlDep = new System.Web.Caching.SqlCacheDependency("cacheSql", "BlogUser");
    
                    //设置缓存
                    HttpRuntime.Cache.Add(
                        "time",//
                        obj,//
                        sqlDep,//依赖项
                        System.Web.Caching.Cache.NoAbsoluteExpiration,//没有绝对过期时间
                        System.Web.Caching.Cache.NoSlidingExpiration,//没有相对过期时间
                        System.Web.Caching.CacheItemPriority.Normal,//失效策略
                        null//回调函数
                        );
                    Response.Write("缓存设置成功:" + obj);
                }
                else
                {
                    Response.Write("缓存的时间为:" + HttpRuntime.Cache["time"].ToString());
                }

    数据库缓存依赖需要在web.config中进行设置

    <configuration>
      <connectionStrings>
        <!-- 数据库连接字符串的名称-->
        <add name="conn" connectionString="server=.;database=OumindBlog;uid=sa;pwd=master"/>   
      </connectionStrings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <caching>
          <sqlCacheDependency>
            <databases>
              <!--      连接字符串的名称       当前节点的名字  推送时间-->
              <add connectionStringName="conn" name="cacheSql" pollTime="10000" />
            </databases>
          </sqlCacheDependency>
        </caching>
      </system.web>
    </configuration>

    并且需要提前通过vs开发人员命令配置命令

    aspnet_regsql -C "数据库连接字符串" -ed -et -t "依赖的表名"
  • 相关阅读:
    Java jar: A JNI error has occurred, please check your installation and try again
    iOS开发UITouch触摸API简介
    iOS 绘制图形
    iOS图形绘制
    iOS利用drawRect方法绘制图形
    iOS 混合变换旋转 CGAffineTransform 的使用
    [原创]什么是持续集成,持续交付和持续部署?
    [原创]Windows下Python+Pytest+Allure 接口自动化测试环境安装
    Embeded linux 之 UBIFS文件系统【转】
    ARM嵌入式学习笔记——《根文件系统》【转】
  • 原文地址:https://www.cnblogs.com/ianism/p/4342329.html
Copyright © 2011-2022 走看看