Asp.Net Cache

mybwu_com發表於2013-12-24

Asp.net Cache


Output Cache

Usage

·Cache all request: <%@ OutputCache Duration=”20” varybyparam =”none” %>

·<varybyparam= “*”>: will cache for each request with query string

·<varybyparam=”ProductId;ProductName”>: cache request with special query string

·Vary by custom:

a. <varybyparam=”none”varybycustom=”browser” >

b.in global.asax file:

public override string GetVaryByCustomString(HttpContext context, string tag){

if(tag==”browser”){ return context.Request.Browser.Browser;}

}

This will cache for different browser.

·Cacheby header :

<%@varybyparam =”none” varybyheader=”accept_product” %>

Scenario Example (varybyparam=”*”)

1.Request a page without parameter -> store page copy A

2.Request page pass Product id = 1 -> store page copy B

3.Request page Pass product id = 2 -> store page copy C

4.Request page Pass product id = 1 -> if copy B not expired , return copy B

5.Request page without parameter -> if copy A not expired , return copy A

Cache Profile and Configuration

Step 1:

<System.Web>

<Caching>

<OutputCacheSettings><OutputCacheProfiles>

<addname=”productCache” duration=”60”> -- seconds

</OutoutCacheProfiles></OutputCacheSettings></Cache></System.Web>

Step 2:

<%@OutputCache CacheProfile=”ProductCache” varybyparam=”none” %>

Cache Configuration

<System.Web><Caching><Cache

disableMemoryCollection=”true/false”– when memory is low need disable or not

disableExpiration=”true/false”

PercentagePhysicalMemoryUsedLimit=”90”—defaultis 90%

PrivateBytesLimit=”0”

privateBytesPoolTime=”00:2:00”

>

By default,Cache is stored in Cache Asp.net Process, so faster

For deploystrategy is not distribution system, suggest use this setting .

Distribute System Cache

1.Storein disk – suggest store in SSD for high speed I/O

2.SqlServer

3.Windowsserver App Fabric

Custom Cache

For some reason (Distribute system), need to build a custom Cache

1.Implement a Cache provider class by inherit from OutputCacheProvider

FileCacheProvider:OutputCacheProvider{

}

2.Config:

<System.Web><Caching><OutputCache defaultProvider=”FileCache”>

<Providers><add name =”FileCache” type=”FileCacheProvider”CachePath=”~/~” />

</Providers><…>

Or just override a method in global file :

Public override string GetOutputCacheProviderName(HttpContext context){

//implementation

}

Data Cache

Page Data Cache

Global toall requests

Thread safe

If expires,automatically remove

Can set dependence to a file or db object, once dependency object changes, cache will be removed automatically

Expiration policy

Sliding expiration: 10 min – if not used within 10 min, then remove

Absolution expiration:10 min – after 10min , remove

Code:

Slide Expiration:

Cache.Insert(“item”,obj,null, DateTime.Max, TimeSpan.FromMin(10));

Absolute Expiration:

Cache.Insert(“item”,obj,null, DateTime.Now.AddMin(60), TimeSpan.Zero);

Cache Dependency

File

Db Object

Message Queue

Cache dependencynotification

Sql server

1.sql server data table changed

2.affects a registered command

3.notification msg

Msmq

1.Cache object set dependency in MSMQ

2.Changes happen in MSMQ

3.Notify cache

Resource Cache

Cache by IIS .

相關文章