次の方法で共有


[方法] VaryByCustom イベント ハンドラを使用してキャッシュを拡張する

IVaryByCustomHandler インターフェイスを作成して、キャッシュの拡張と操作を実行できます。ASP.NET キャッシュ機能である VaryByCustom を使用すると、カスタム文字列に基づいて、複数のバージョンのページ出力をキャッシュすることができます。たとえば、各ユーザーに異なるウェルカム文字列を表示するキャッシュ ページを表示したい場合、VaryByCustom を使用できます。ASP.NET の VaryByCustom の詳細については、MSDN の「Page Output Caching, Part 1 (英語)」を参照してください。

このトピックでは、Microsoft Office SharePoint Server 2007 のキャッシュを拡張する VaryByCustom プロパティを使用するために必要な 3 つの基本ステップについて説明します。

  • 返されるページ上のコンテンツを変更するために出力キャッシュが使用する、カスタム文字列のリストを示す VaryByCustom ハンドラを作成します。

  • Global.asax ファイルにハンドラを登録します。

  • カスタム文字列を定義して、Web アプリケーションのキャッシュ プロファイル向けに変更します。システムは、キャッシュを変更する方法を決定する VaryByCustom ハンドラに、これらの文字列を渡します。

サンプル コードでは、クライアント ブラウザがカスケード スタイル シート (CSS) をサポートしているかどうか、現在のユーザーが管理者かどうかという 2 つのパラメータに基づいてコンテンツが異なります。GetVaryByCustomString メソッドに渡されるカスタム文字列の値に応じて、関数はこれらのパラメータのいずれかまたは両方に基づく文字列、またはどちらにも基づかない文字列を構成します。キャッシュ システムでは、GetVaryByCustomString が返す各値とは異なるキャッシュを作成します。たとえば、custom の値が SupportsCSS の場合、GetVaryByCustomString は、True または False を含む文字列を返します。これは、sb.Append(context,Request.Browser.SupportsCss.ToString) の結果が CSS をサポートしているか、していないかを示しています。

Web アプリケーションの各キャッシュ プロファイル向けのカスタム文字列の値は、次のコード例に示すように指定することができます。

[C#]

//First, write a VaryByCustom handler. This example varies based on
//two parameters: whether the client browser supports CSS,
//and whether the current user is an administrator.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.ApplicationRuntime;
using Microsoft.SharePoint;

namespace CacheDemo
{
    public class CacheDemoHttpApplication ; SPHttpApplication, IVaryByCustomHandler
    {
        internal const string MyVaryByString1 = "SupportsCSS";
        internal const string MyVaryByString2 = "SiteAdmin";
        public override void Init()
        {
            base.Init();
            this.RegisterGetVaryByCustomStringHandler((Microsoft.SharePoint.ApplicationRuntime.IVaryByCustomHandler)this);
        }

        public string GetVaryByCustomString(HttpApplication app, HttpContext context, string custom)
        {
            //The code looks for parameters specified in the cache 
            //profile that are passed to the handler and parses those 
            //delimited by a semicolon.
            StringBuilder sb = new StringBuilder();
            string[] strings = custom.Split(';');
            bool appended = false;
            foreach (string str in strings)
            {
                switch (str)
                {
                    case MyVaryByString1:
                        if (appended)
                        {
                            sb.Append(';');
                        }
                        //If you want to vary based on a property of
                        //the request, work with the http context
                    sb.Append(context.Request.Browser.SupportsCss);
                        break;
                    case MyVaryByString2:
                        if (appended)
                        {
                            sb.Append(';');
                        }
                        //If you want to vary by whether the current
                        //user is the site administrator,
                        //examine the SPContext
                        sb.Append(SPContext.Current.Web.UserIsSiteAdmin.ToString());
                        break;

                    default:
                        continue;
                }
                appended = true;
            }
            return sb.ToString();
        }
    }
}

次に、Global.asax ファイルに VaryByCustom イベント ハンドラを登録します。アセンブリ タグを変更して、上記のコードで構築するアセンブリを指定する必要があります。

//Register the VaryByCustom string in the Global.asax file
<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Assembly Name="cachedemo"%>
<%@ Import Namespace="cachedemo" %>
<%@ Application Language="C#" Inherits="cachedemo.CacheDemoHttpApplication" %>

最後に、Web アプリケーションの各キャッシュ プロファイルに対して、カスタム文字列の値を指定します。

サイト コレクション内の各キャッシュ プロファイルを編集します。

  1. サイト コレクションのルート サイトに移動します。

  2. [サイトの操作] をクリックし、[サイトの設定] をポイントして、[すべてのサイト設定の変更] をクリックします。

  3. [サイト コレクション] セクションの [サイト コレクションのキャッシュ プロファイル] コレクションをクリックします。

  4. 変更するキャッシュ プロファイルをポイントし、右クリックしてから [編集] をクリックします。

  5. SupportsCSS;SiteAdmin など、この例で使用するために [ユーザー設定のパラメータごとにキャッシュ] フィールドに追加するカスタム文字列を入力して、[OK] をクリックします。

See Also

概念

出力キャッシュとキャッシュ プロファイル

カスタム キャッシュの概要

オブジェクト キャッシュ