티스토리 뷰

 

Cache Service – ASP.NET Session state

 

Windows Azure에서 웹 사이트, 클라우드 서비스, 가상 컴퓨터의 여러 인스턴스에 걸쳐 캐시 서비스를 통해 개체 캐시, 세션, Output 캐시 등을 손쉽게 지원해줄 수 있습니다.

 

1.     먼저 캐시 서비스를 생성하는 내용을 알아보면 Windows Azure 관리 포털의 새로 만들기에서 데이터 서비스, CACHE 를 선택하고 QUICK CREATE 를 클릭합니다. 그러면 아래와 같은 그림에서 ENDPOINT 와 지역, 메모리를 선택할 수 있습니다.
 

메뉴가 비활성화되어 있다면 미리 보기 기능에서 활성화 해볼 수 있습니다.

2.     캐시를 생성하고 난 후 해당 ENDPOINT 를 클릭해보면 자세한 정보를 확인 가능합니다. 그 중에서 Configure 메뉴를 보면 아래와 같이 나타납니다. 이 메뉴의 요소 중에서 High Availability 메뉴는 생성시나 Scale 메뉴에서의 Cache OfferingPremium으로 선택하면 됩니다.
 

3.     자세한 요소에 대한 내용은 캐시의 일반적인 특징과 유사합니다.

Name: Basicdefault 라는 이름 하나만 제공되며 Standard, Premium은 총 10개까지 생성

Expiry Policy: 캐시가 만료되는 정책으로 Absolute, Sliding, Never

Time(Min): 계시가 지속되는 시간으로 기본값 10

Notification: 캐시이 변경이 일어날 때 비동기적인 알림을 지원

High Availability: Premium에서만 제공되는 고 가용성

Eviction: 캐시내에 메모리에 공간을 유지하게 하는 것으로 기본값 Enable

 

4.     위의 캐시를 ASP.NET 에서 이용해보면 아래와 같습니다. 기존 프로그램에서 사용하던 방식 그대로 사용이 가능합니다. Object Cache, Output Cache, Session 등이며 Web.Config와 참조하는 어셈블리에 대한 내용이 변경됩니다.

5.     최신 Windows Azure SDK를 설치하였다면 ASP.NET 프로젝트를 생성하고 솔루션 탐색기에서 Nuget 패키지 관리 메뉴를 통해 ‘WindowsAzure.Caching’ 을 검색합니다. Windows Azure Caching을 선택하고 설치를 클릭합니다. 어셈블리를 추가하고 Config를 구성합니다.

 

6.     위 패키지를 설치하고 나면 Web.Config가 아래와 같이 수정됩니다.

<!-- Windows Azure Caching session state provider -->

    <!--<sessionState mode="Custom" customProvider="AFCacheSessionStateProvider">

      <providers>

        <add name="AFCacheSessionStateProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" applicationName="AFCacheSessionState"/>

      </providers>

    </sessionState>-->

    <!-- Windows Azure Caching output caching provider -->

    <!--Uncomment this section to use Windows Azure Caching for output caching-->

    <!--<caching>

      <outputCache defaultProvider="AFCacheOutputCacheProvider">

        <providers>

          <add name="AFCacheOutputCacheProvider" type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" applicationName="AFCacheOutputCache" />

        </providers>

      </outputCache>

    </caching>-->

</system.web>

 

 

  <dataCacheClients>

    <dataCacheClient name="default">

      <!--To use the in-role flavor of Windows Azure Caching, set identifier to be the cache cluster role name -->

      <!--To use the Windows Azure Caching Service, set identifier to be the endpoint of the cache cluster -->

      <autoDiscover isEnabled="true" identifier="[Cache role name or Service Endpoint]" />

      <!--<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />-->

      <!--Use this section to specify security settings for connecting to your cache. This section is not required if your cache is hosted on a role that is a part of your cloud service. -->

      <!--<securityProperties mode="Message" sslEnabled="false">

        <messageSecurity authorizationInfo="[Authentication Key]" />

      </securityProperties>-->

    </dataCacheClient>

</dataCacheClients>

 

7.     주석부분에 sessionState, outoutCache에 대한 내용이 있으므로 원하는 내용에 따라 해제합니다.

8.     autoDiscover 태그의 identifier 부분의 값을 포털의 캐시 대시보드를 클릭하여 ENDPOINT URL 로 변경합니다. (hcache.cache.windows.net)

9.     securityProperties 태그의 authorization 부분의 값을 에는 포털의 캐시 대시보드를 클릭하여 Manage Keys 를 클릭하여 키 값을 복사한 후 변경합니다.

10.   코드에서는 일반적인 Session 코드를 사용하면 됩니다.

Session["sValue"] = "testKey";

 

        string sValue="";

        object obj = Session["sValue"];

        if (obj != null)

            sValue = Session["sValue"].ToString();

 

11.     Windows Azure에 게시후 테스트해보시면 제대로 동작하는 것을 확인이 가능합니다. localhost로는 로딩만 되어 게시 해보면 바로 나오는 것을 확인할 수 있습니다.

 

간략히 캐시 서비스를 이용하는 것을 알아보았습니다. Nuget을 이용한 설정만 다르며 기존 캐시를 사용하던 것과 동일하게 사용이 가능합니다. 그럼 캐시의 용량을 어떻게 설정하는 것이 적절할 까요? 아래 링크를 참고하여 용량 설계시 참고하십시오.

http://msdn.microsoft.com/library/WindowsAzure/dn386139.aspx

 

댓글