Redis RedisOutputCacheProvider

Jan 21, 2015

2 mins read

先開個Web專案

[![](http://3.bp.blogspot.com/-W0eH1UB-UAg/VL9vePbpb0I/AAAAAAAACI4/dA1ZpbAc2uo/s1600/01.%E9%96%8B%E6%96%B0%E5%B0%88%E6%A1%88.png)](http://3.bp.blogspot.com/-W0eH1UB-UAg/VL9vePbpb0I/AAAAAAAACI4/dA1ZpbAc2uo/s1600/01.%E9%96%8B%E6%96%B0%E5%B0%88%E6%A1%88.png)
安裝Microsoft.Web.RedisOutputCacheProvider套件
[![](http://4.bp.blogspot.com/-QT6Acrh4AnY/VL9106aZdnI/AAAAAAAACJg/7SwSvzSuaow/s1600/02.%E5%AE%89%E8%A3%9D%E5%A5%97%E4%BB%B6.png)](http://4.bp.blogspot.com/-QT6Acrh4AnY/VL9106aZdnI/AAAAAAAACJg/7SwSvzSuaow/s1600/02.%E5%AE%89%E8%A3%9D%E5%A5%97%E4%BB%B6.png)
web.config中的caching區段會自動帶入預設的設定 註解的說明也很清楚
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <caching>
      <outputCache defaultProvider="MyRedisOutputCache">
        <providers>
          <!-- Either use 'connectionString' and provide all parameters as string OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
          <!-- 'databaseId' and 'applicationName' can be used with both options. -->
          <!--
          <add name="MyRedisOutputCache" 
            host = "127.0.0.1" [String]
            port = "" [number]
            accessKey = "" [String]
            ssl = "false" [true|false]
            databaseId = "0" [number]
            applicationName = "" [String]
            connectionTimeoutInMilliseconds = "5000" [number]
            operationTimeoutInMilliseconds = "1000" [number]
            connectionString = "<valid StackExchange.Redis connection string>" [String]
          />
          -->
          <add name="MyRedisOutputCache" type="Microsoft.Web.Redis.RedisOutputCacheProvider" host="10.9.11.64" accessKey="" ssl="false" />
        </providers>
      </outputCache>
    </caching>
  </system.web>
</configuration>
隨便輸出點東西
namespace WebApplication1
{
    using System;
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(DateTime.Now);
    }
}

}

頁面啟用快取

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<%@ OutputCache Duration="60" VaryByParam="*" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml”> <head runat="server”> <meta http-equiv="Content-Type” content="text/html; charset=utf-8”/> <title></title> </head> <body> <form id="form1” runat="server”> <div>

&lt;/div&gt;
&lt;/form&gt;

</body> </html>

網頁輸出的結果

[![](http://1.bp.blogspot.com/-by2w238QgVI/VL92f8VkUzI/AAAAAAAACJo/lpbT_JMRf7A/s1600/03.%E7%B6%B2%E9%A0%81%E7%B5%90%E6%9E%9C.png)](http://1.bp.blogspot.com/-by2w238QgVI/VL92f8VkUzI/AAAAAAAACJo/lpbT_JMRf7A/s1600/03.%E7%B6%B2%E9%A0%81%E7%B5%90%E6%9E%9C.png)
Redis上面存在的資料
[![](http://4.bp.blogspot.com/-banUlA3Yokc/VL92jLW0W6I/AAAAAAAACJw/HwN6RslnBD4/s1600/04.Redis%E7%B5%90%E6%9E%9C.png)](http://4.bp.blogspot.com/-banUlA3Yokc/VL92jLW0W6I/AAAAAAAACJw/HwN6RslnBD4/s1600/04.Redis%E7%B5%90%E6%9E%9C.png)

Sharing is caring!