zoukankan      html  css  js  c++  java
  • ASP 缓存处理及URL 重写

    1 缓存

    1.1.1

    <%--通过设置VaryByParam ="none" 来实现 整页缓存 --%> 
    <%@ OutputCache Duration="5" VaryByParam ="none" %>

    1.1.2

    <%--带参数缓存,只要包含在VaryByParam 中的任何一个参数改变都会使页面缓存失效,如果当前参数不包含在VaryByParam中,则改变也无效--%> 
    <%@ OutputCache Duration="10" VaryByParam="id;name;age" %>

    1.1.3

    <%--根据控件id来缓存整个页面--%>
    
    <%@ OutputCache Duration="10" VaryByControl="a" %>

    1.2 【Wed.config】 配置 缓存

    <%@ OutputCache CacheProfile="MyCacheTest"%> 【Wed.config】 下的配置 
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    <!--统一页面缓存管理配置节点-->
      <caching>
        <outputCacheSettings>
          <outputCacheProfiles>
          <!--duration:缓存事时间 name:缓存名称,在页面使用的时候 CacheProfile要指定name中设置的值-->
            <add name="MyCacheTest" duration="2" varyByParam="none" />
           </outputCacheProfiles>
        </outputCacheSettings>
      </caching>

    1.3 文件缓存 在asp中的后台代码:    

     1    protected void Page_Load(object sender, EventArgs e)
     2         {
     3             string atxtContent;
     4 
     5             if (Cache["datetimenow"] != null)
     6             {
     7                 //lbdatetime.Text = Cache["datetimenow"].ToString();
     8                 lbdatetime.Text = Cache["datetimenow"].ToString();
     9             }
    10             else
    11             {
    12                 DateTime now = DateTime.Now;
    13                 #region 设置缓存的绝对过期时间
    14                 Cache["datetimenow"] = now.ToString();
    15                 //设置缓存的绝对过期时间  中不设置相对过期时间 写法一:TimeSpan.Zero
    16                 Cache.Insert("datetimenow", now, null, DateTime.Now.AddSeconds(10), TimeSpan.Zero);
    17                 //设置缓存的绝对过期时间  中不设置相对过期时间 写法二:System.Web.Caching.Cache.NoSlidingExpiration
    18                 Cache.Insert("datetimenow", now, null, DateTime.Now.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration);  
    19                 #endregion
    20 
    21                 #region 设置缓存的相对过期时间
    22                 //设置缓存的相对过期时间
    23                 Cache.Insert("datetimenow", now, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 0, 10)
    24                   , System.Web.Caching.CacheItemPriority.High, myCacheItemRemovedCallback); 
    25                 #endregion
    26 
    27                 #region 文件缓存依赖,首先:绝对过期时间和相对过期时间要设置不过期
    28                 //文件缓存依赖,首先:绝对过期时间和相对过期时间要设置不过期
    29                 // 定义该缓存要依赖的文件,注意传入文件的绝对路径
    30 
    31                 atxtContent = System.IO.File.ReadAllText(Server.MapPath("b.txt"));
    32                 CacheDependency cdFile = new CacheDependency(Server.MapPath("a.txt"));
    33 
    34                 Cache.Insert("datetimenow"
    35                     , atxtContent
    36                     , cdFile
    37                     , System.Web.Caching.Cache.NoAbsoluteExpiration
    38                     , System.Web.Caching.Cache.NoSlidingExpiration
    39                     , System.Web.Caching.CacheItemPriority.High, myCacheItemRemovedCallback
    40                     ); 
    41                 #endregion
    42 
    43                 lbdatetime.Text = atxtContent;
    44             }
    45 
    46         }
    47 
    48         #region 当缓存被移除的时候,系统会自动调用该回调函数  +myCacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
    49         /// <summary>
    50         ///当缓存被移除的时候,系统会自动调用该回调函数
    51         /// </summary>
    52         /// <param name="key">当前失效的缓存key</param>
    53         /// <param name="value">当前失效时缓存key对应的缓存内容</param>
    54         /// <param name="reason">缓存失效的原因</param>
    55         void myCacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
    56         {
    57             System.IO.File.WriteAllText(@"E:传智播客7期班2013-9-15 asp.net高级 缓存,工厂,单例源代码缓存WebApplication1log.txt"
    58                 , "key=" + key + "  value=" + value + " reason=" + reason);
    59         } 
    60         #endregion
    View Code

    1.4 数据库缓存 直接在【Web.config】中配置

     1 <configuration>
     2   <connectionStrings>
     3     <add name="CachedbConnectString" connectionString="data source=.;initial catalog=PhoneBook;user id=sa;password=123" providerName="System.Data.SqlClient"/>
     4   </connectionStrings>
     5     <system.web>
     6       <compilation debug="true" targetFramework="4.0" />
     7 <!--统一页面缓存管理配置节点-->
     8       <caching>
     9         
    10         <sqlCacheDependency enabled="true" pollTime="6000">
    11           <databases>
    12             <add name="PhoneBook" connectionStringName="CachedbConnectString"/>
    13           </databases>
    14         </sqlCacheDependency>
    15         
    16       </caching>
    17       
    18     </system.web>
    19 
    20   
    21 </configuration>

    URL重写

    方法一:

    新建一个 【Global.asax】文件 在它的  Application_BeginRequest 方法里写 获取URL,并通过 正则表达式 将 URL 重写

     //得到当前url的请求路径
                string currUrl = HttpContext.Current.Request.RawUrl; //top/1
                string newUrl = "";
    
                //定义正则表达式
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("index/(.*)/(.*)");
                if (reg.IsMatch(currUrl))
                {
                    //将匹配的url替换成"index.aspx?id=$1&name=$2" 类型的字符串
                    newUrl = reg.Replace(currUrl, "index.aspx?id=$1&name=$2");
    
                    //重写是靠RewritePath(path)  path只能传相对路径index.aspx?id=1&name=2 而不能传http://loclhost/index.aspx?id=1&name=2 
                    HttpContext.Current.RewritePath(newUrl);


    方法二:在Internet 中选中网站后

    在URL 重写中 --添加规则 --用户友好URL

     

                

    在这里就是将你要修改的URL写到URL 输入框里,下面的下拉框选择你要的URL格式

      如果有漏的,希望有大神补充一下!!

  • 相关阅读:
    C++开源库,欢迎补充。
    LeetCode第二题
    LeetCode第五十八题
    tomcat连接数据库oracle问题,ClassNotFoundException异常
    jsp验证码,解决无法更新验证码问题
    [面试真题] LeetCode:Symmetric Tree
    [面试真题] LeetCode:Flatten Binary Tree to Linked List
    [面试真题] LeetCode:Longest Common Prefix
    [面试真题] LeetCode:Reverse Linked List II
    [面试真题] LeetCode:Merge Two Sorted Lists
  • 原文地址:https://www.cnblogs.com/jhy55/p/3372907.html
Copyright © 2011-2022 走看看