捲土重來之staticHtml基礎使用教程

前段時間發佈了一個asp.net生存html緩存的東西,老實說坑了蠻多的人,bug比較多, 通過這段時間的測試與改進,應該到了可使用的地步了,

歡迎你們測試與使用,下面我介紹使用教程,對了,這裏感謝一下猴子,他幫我測試了不少。html

首先請將StaticHtml.dll添加引用,或者拷貝到bin目錄中,下面是具體配置說明

注意:下面的配置步驟都是在web.config中操做。git

1. 添加StaticHtml配置文件讀取節點

在configuration/configSections添加以下節點github

<section name="staticHtml" type="StaticHtml.StaticHtmlSection,StaticHtml"/>web

添加StaticHtml正則表達式

2. 添加HttpModule

在configuration/system.web/httpModules中,添加瀏覽器

<add name="staticHtml" type="StaticHtml.HttpModule,StaticHtml"/>緩存

若是這樣配置,用瀏覽器訪問任意一個頁面,若是根目錄下沒有生成staticHtml_log.txt這個文件,請將上面的節點,添加到configuration/system.webServer/httpModule中服務器

固然,請確保你服務器能正確加載httpmodule,由於有可能你的服務器配置,根本就不能加載任何httpmodule。請參考asp.net mvc配置mvc

3. 配置本身站點的生成緩存規則

在configuration中添加以下節點asp.net

<staticHtml skip="admin/" run="on">

<rule name=" index ">

<patten type="StaticHtml.RegexPatten,StaticHtml" pars="RegPatten=index \.aspx"/>

<store type="StaticHtml.MemStore,StaticHtml"/>

<genKey type="StaticHtml.UrlMd5GenKey,StaticHtml"/>

<expire type="StaticHtml.TimeExpire,StaticHtml"  pars="Second=300"/>

</rule>

<rule name="content">

<patten type="StaticHtml.RegexPatten,StaticHtml" pars="RegPatten=article/"/>

<store type="StaticHtml.FileStore,StaticHtml" pars="Path=cacheHtml_article/"/>

</rule>

</staticHtml>

上面的配置,定義了2個規則,

第一個規則,

<rule name=" index ">

<patten type="StaticHtml.RegexPatten,StaticHtml" pars="RegPatten=index\. aspx "/>

<store type="StaticHtml.MemStore,StaticHtml"/>

</rule>

規則,這個規則的名字叫作index,

patten節點 若是url中匹配index\. aspx 這個正則表達式,則緩存。

store節點 將緩存的html保存到內存中

genKey 節點 將request.rawRul經過md5加密生成key,在statichtml很是重要,表明了一個惟一的緩存頁面,

expire節點 意味着緩存300秒(5分鐘)

第二個規則

<rule name="content">

<patten type="StaticHtml.RegexPatten,StaticHtml" pars="RegPatten=article/"/>

<store type="StaticHtml.FileStore,StaticHtml" pars="Path=cacheHtml_article/"/>

</rule>

這個規則的名字叫作content(規則名字不能重複)

patten 節點 若是url中匹配article/這個正則表達式的,就緩存

store節點 將緩存的保存到根目錄下面/cacheHtml_article中

StaticHtml節點 skip屬性 admin/ 表明若是url能匹配這個正則表達式,則不緩存。

提示:staticHtml節點skip 屬性,patten節點的 pars屬性, 都是能夠配置正則表達式的, 就是一個純粹的正則表達式,能夠寫的很複雜,也能夠很簡單。

4. 其餘補充

1:下篇文字我將詳細介紹如何擴展statichtml

2:下載statichtml.dll(這是最新版,使用gzip壓縮存儲,大大減小磁盤佔用,網站流量。)

3:源碼仍是在github上https://github.com/tianqiq/StaticHtml

4:經網友測試發現,在4.0,4.5 .net上會出現刷新後,就空白的狀況。因此請將項目的.net版本設置爲3.5,便可。(真沒想到這麼多童鞋是用4.0甚至4.5了)

 

 

更新:

如今已經修復4.0以上刷新後,空白的狀況, 歡迎測試與使用。(2013/7/21)

代碼已經更新到github上, 請從這裏下載最新dll http://url.cn/H5TSeH

相關文章
相關標籤/搜索