Enterprise Library Caching Application Block Brian Button Software Design Engineer Ron Jacobs...

41
Enterprise Library Enterprise Library Caching Application Caching Application Block Block Brian Button Brian Button Software Design Engineer Software Design Engineer Ron Jacobs Ron Jacobs Product Manager Product Manager Scott Scott Densmore Densmore Software Design Software Design Engineer Engineer

Transcript of Enterprise Library Caching Application Block Brian Button Software Design Engineer Ron Jacobs...

Enterprise LibraryEnterprise LibraryCaching Application BlockCaching Application Block

Brian Button Brian Button Software Design EngineerSoftware Design Engineer

Ron JacobsRon JacobsProduct ManagerProduct Manager

Scott DensmoreScott DensmoreSoftware Design Software Design EngineerEngineer

AgendaAgenda

Caching architectural guidance Caching architectural guidance from from patterns & practicespatterns & practices

Describe implementation of Describe implementation of caching scenarios and guidance caching scenarios and guidance using the Caching Application using the Caching Application BlockBlock

DemonstrationDemonstration

QuestionsQuestions

Sound familiar?Sound familiar?

Users are complaining about Users are complaining about application performanceapplication performance

You have already spent the next You have already spent the next year’s IT budget scaling out your year’s IT budget scaling out your database servers to handle the database servers to handle the amount of requests for dataamount of requests for data

Your application cannot perform, or Your application cannot perform, or is severely degraded, without a live is severely degraded, without a live connection to the backend data connection to the backend data sourcesource

Why Cache?Why Cache?

PerformancePerformanceStoring relevant data as close as possible to the Storing relevant data as close as possible to the data consumer avoids repetitive data creation, data consumer avoids repetitive data creation, processing, and transportationprocessing, and transportation

ScalabilityScalabilityAvoid wasting resources processing the same data, Avoid wasting resources processing the same data, business functionality, and user interface business functionality, and user interface fragments required by multiple users and fragments required by multiple users and processesprocesses

Reduce database requests, allowing more users to Reduce database requests, allowing more users to be servedbe served

AvailabilityAvailabilityStoring that data in another place, your application Storing that data in another place, your application may be able to survive system failures such as may be able to survive system failures such as network latency, Web service problems, or network latency, Web service problems, or hardware failureshardware failures

Sample Application Sample Application Caching ScenariosCaching Scenarios

You are creating a smart client You are creating a smart client application that uses locally cached application that uses locally cached reference data to create requests and reference data to create requests and support offline operationssupport offline operations

You are creating a Windows Service or You are creating a Windows Service or Console application which needs a Console application which needs a cache to improve performancecache to improve performance

Caching Application Caching Application BlockBlock

Provides a flexible and extensible caching Provides a flexible and extensible caching mechanism that can be used at all layers mechanism that can be used at all layers of an applicationof an application

Supports backing stores that persist Supports backing stores that persist cache data into a database or isolated cache data into a database or isolated storage, so the data can survive app storage, so the data can survive app restartsrestarts

Easy to useEasy to use

Easy to configure, using the Enterprise Easy to configure, using the Enterprise Library Configuration ToolLibrary Configuration Tool

Thread-safeThread-safeEnsures that the states of the in-memory Ensures that the states of the in-memory cache and the backing store remain cache and the backing store remain synchronized. synchronized.

Exploring the Caching Exploring the Caching Application BlockApplication Block

Creating the Caching Application Creating the Caching Application Block configurationBlock configurationSelecting a backing storeSelecting a backing storeCreating the cacheCreating the cacheAdding an item to the cacheAdding an item to the cacheSetting the expiration policySetting the expiration policyRetrieving an item from the cacheRetrieving an item from the cacheImplementing cache loading Implementing cache loading strategiesstrategiesRemoving an item from the cacheRemoving an item from the cacheFlushing the cacheFlushing the cacheKey extensibility pointsKey extensibility points

Add the Caching Application Block to your application configurationAdd the Caching Application Block to your application configuration

Create a cache manager for each set of data to be cachedCreate a cache manager for each set of data to be cached

Designate one as the default cache managerDesignate one as the default cache manager

Creating ConfigurationCreating Configuration

Cache Storage GuidanceCache Storage Guidance

Memory resident cacheMemory resident cache. Memory-. Memory-based caching is usually used when: based caching is usually used when:

An application is frequently using the same An application is frequently using the same datadata

An application often needs to reacquire the An application often needs to reacquire the datadata

Disk resident cacheDisk resident cache. Disk based . Disk based caching is useful when: caching is useful when:

You are handling large amounts of dataYou are handling large amounts of data

Data in the application services (for Data in the application services (for example, a database) may not always be example, a database) may not always be available for reacquisition (for example, in available for reacquisition (for example, in offline scenarios)offline scenarios)

Cached data lifetime must survive process Cached data lifetime must survive process recycles and computer rebootsrecycles and computer reboots

Caching Architecture Guide for .NET Framework Applications

Caching Application Block Caching Application Block StorageStorage

Cache always exists in memoryCache always exists in memory

Cache always has a backing store Cache always has a backing store Null backing store (in-memory only, not Null backing store (in-memory only, not persistent)persistent)

Persistent storage Persistent storage

Persistent backing storesPersistent backing storesUseful when cached data lifetime must Useful when cached data lifetime must survive process recycles and computer survive process recycles and computer rebootsreboots

Isolated storage, Data Access Application Isolated storage, Data Access Application BlockBlock

Contents always match in-memory cacheContents always match in-memory cache

In-memory cache is loaded from backing In-memory cache is loaded from backing store during cache initialization store during cache initialization

Backing Store Backing Store ConfigurationConfiguration

Isolated StorageIsolated StorageSegregated by user and assemblySegregated by user and assembly

Caching Application Block provides Caching Application Block provides partition to segregate multiple cache partition to segregate multiple cache managers within an applicationmanagers within an application

Backing Store Backing Store ConfigurationConfigurationData Access Application BlockData Access Application Block

Segregated by database instance and Segregated by database instance and named partitionnamed partition

Require configuring the Data Access Require configuring the Data Access Application BlockApplication Block

Backing Store Backing Store ConfigurationConfigurationEncrypting cache data in the Encrypting cache data in the

backing storebacking storeRequires Cryptography Application Requires Cryptography Application BlockBlock

Select Symmetric provider to useSelect Symmetric provider to use

Create the default cache managerCreate the default cache manager

Creating the Cache in Creating the Cache in CodeCode

CacheManager myCache = CacheManager.GetCacheManager();

Create the cache manager named “Products”Create the cache manager named “Products”

CacheManager productsCache = CacheManager.GetCacheManager(“Products”);

Adding an Item to the Adding an Item to the CacheCacheAdd an item to the cache with defaultsAdd an item to the cache with defaults

productsCache.Add(“ProductID123”, productObject);

DefaultsDefaultsScavenging priority: NormalScavenging priority: Normal

No expirationNo expiration

NotesNotesAdding a second item with the same key as an Adding a second item with the same key as an existing item replaces the existing itemexisting item replaces the existing item

When configured to use a persistent backing When configured to use a persistent backing store, objects added to the cache must be store, objects added to the cache must be serializableserializable

Determining an Determining an Expiration PolicyExpiration Policy

Time-based expirations Time-based expirations

Invalidate data based on either relative Invalidate data based on either relative or absolute time periodsor absolute time periods

For use when volatile cache items—such For use when volatile cache items—such as those that have regular data as those that have regular data refreshes or those that are valid for only refreshes or those that are valid for only a set amount of time—are stored in a a set amount of time—are stored in a cache.cache.

Notification-based expirationsNotification-based expirations

Validity of a cached item based on the Validity of a cached item based on the properties of an application resource, properties of an application resource, such as a file, a folder, or any other type such as a file, a folder, or any other type of data source.of data source.

Time-Based ExpirationsTime-Based Expirations

AbsoluteAbsolute. Allows you to define the . Allows you to define the lifetime of an item by specifying the lifetime of an item by specifying the absolute time for an item to expire. absolute time for an item to expire.

SimpleSimple—You define the lifetime of an item by —You define the lifetime of an item by setting a specific date and time for the item to setting a specific date and time for the item to expire. expire.

ExtendedExtended—You define the lifetime of an item —You define the lifetime of an item by specifying expressions such as every by specifying expressions such as every minute, every Sunday, expire at 5:15 AM on minute, every Sunday, expire at 5:15 AM on the 15th of every month, and so on. the 15th of every month, and so on.

SlidingSliding. Allows you to define the lifetime . Allows you to define the lifetime of an item by specifying the interval of an item by specifying the interval between the item being accessed and the between the item being accessed and the policy defining it as expired. policy defining it as expired.

Time-Based ExpirationsTime-Based Expirations

DateTime refreshTime = new DateTime(2005, 3, 21, 2, 0, 0);AbsoluteTime expireTime = new AbsoluteTime(refreshTime);

primitivesCache.Add("Key1", "Cache Item1", CacheItemPriority.Normal, null, expireTime);

Requires using extended overload of Add Requires using extended overload of Add methodmethod

Caching Application Block classes support Caching Application Block classes support time-based expirationstime-based expirations

AbsoluteTimeAbsoluteTime

SlidingTimeSlidingTime

ExtendedFormatTimeExtendedFormatTime

Absolute time example: expire at 2:00 AM Absolute time example: expire at 2:00 AM on 3/21/05on 3/21/05

Sliding Time ExpirationsSliding Time Expirations

TimeSpan refreshTime = new TimeSpan(0, 5, 0);SlidingTime expireTime = new SlidingTime(refreshTime);

primitivesCache.Add("Key1", "Cache Item1", CacheItemPriority.Normal, null, expireTime);

Sliding time example: expire if item has not been accessed for 5 Sliding time example: expire if item has not been accessed for 5 minutesminutes

Extended Time Format Extended Time Format ExpirationsExpirations

Extended time formatExtended time format““<Minute> <Hour> <Day of month> <Month> <Minute> <Hour> <Day of month> <Month>

<Day of week>”<Day of week>”

* means run every period* means run every period

ExamplesExamples““* * * * *” expires every minute* * * * *” expires every minute

““5 * * * *” expire 5th minute of every hour5 * * * *” expire 5th minute of every hour

““* 21 * * *” expire every minute of the 21st * 21 * * *” expire every minute of the 21st hour of every day hour of every day

““31 15 * * *” expire 3:31 PM every day31 15 * * *” expire 3:31 PM every day

““7 4 * * 6” expire Saturday 4:07 AM7 4 * * 6” expire Saturday 4:07 AM

““15 21 4 7 *” expire 9:15 PM on 4 July15 21 4 7 *” expire 9:15 PM on 4 July

Extended Time Format Extended Time Format ExpirationsExpirations

ExtendedFormatTime expireTime = new ExtendedFormatTime("0 0 * * 6");

primitivesCache.Add("Key1", "Cache Item1", CacheItemPriority.Normal, null, expireTime);

Extended format example: expire at Extended format example: expire at midnight every Saturdaymidnight every Saturday

Notification-Based Notification-Based ExpirationsExpirations

FileDependency expireNotice = new FileDependency(“Trigger.txt”);

productsCache.Add("Key1", "Cache Item1", CacheItemPriority.Normal, null, expireNotice);

File dependency example: expire if the file Trigger.txt is changedFile dependency example: expire if the file Trigger.txt is changed

You can create custom expirations by creating classes that You can create custom expirations by creating classes that implement ICacheItemExpirationimplement ICacheItemExpiration

Configuring Expiration Poll Configuring Expiration Poll FrequencyFrequency

Removal of expired items occurs on a background threadRemoval of expired items occurs on a background thread

You can set the frequency of how often this thread will run looking for expired itemsYou can set the frequency of how often this thread will run looking for expired items

Item Removal Item Removal NotificationsNotifications

productsCache.Add("Key1", "Cache Item1", CacheItemPriority.Normal, new ProductCacheRefreshAction(),

expireNotice);

Caching Application Block provides Caching Application Block provides notification when an item is removed notification when an item is removed from cachefrom cache

Item has expiredItem has expired

Item was removed explicitly through Item was removed explicitly through codecode

Item was scavengedItem was scavenged

Provide an object that implements Provide an object that implements ICacheItemRefreshAction when item is ICacheItemRefreshAction when item is added to cacheadded to cache

Item Removal Item Removal NotificationsNotifications

[Serializable]public class ProductCacheRefreshAction : ICacheItemRefreshAction{ public void Refresh(string key, object expiredValue, CacheItemRemovedReason removalReason) { // Item has been removed from cache. // Perform desired actions here, // based upon the removal reason (e.g. refresh the cache with the // item). }}

Class implementing ICacheItemRefreshAction must be Class implementing ICacheItemRefreshAction must be marked as Serializable (for persistent backing store)marked as Serializable (for persistent backing store)

Cast returned object to correct typeCast returned object to correct type

Check for null (item not found in cache)Check for null (item not found in cache)

Retrieving an Item from Retrieving an Item from the Cachethe Cache

public Product ReadProductByID(string productID){ Product product = (Product)cache.GetData(productID);

if (product == null) { // Item not in cache }}

Proactive loadingProactive loadingCache is preloaded with items on application startupCache is preloaded with items on application startup

Application response time improves, as all items are in Application response time improves, as all items are in cachecache

Application startup time increases, as all items are loadedApplication startup time increases, as all items are loaded

May cache items that are never requested, using May cache items that are never requested, using unnecessary resources unnecessary resources

Loading typically done on a separate thread, increasing Loading typically done on a separate thread, increasing complexitycomplexity

Reactive loadingReactive loadingCache an item after it is retrieved from the data sourceCache an item after it is retrieved from the data source

Only caches items truly needed (uses less resources)Only caches items truly needed (uses less resources)

Response times slower during application execution if an Response times slower during application execution if an item is not yet cacheditem is not yet cached

Loading the CacheLoading the Cache

Create cache managerCreate cache manager

Loading the Cache Loading the Cache ProactivelyProactively

CacheManager productsCache = CacheManager.GetCacheManager();

// Retrieve the data from the sourceArrayList list = dataProvider.GetProductList();

// Add all the items to the cachefor (int i = 0; i < list.Count; i++) { Product product = (Product) list[i]; productsCache.Add( product.ProductID, product ); }

Add items during component initializationAdd items during component initialization

Loading the Cache Loading the Cache ReactivelyReactively

Product product = (Product) productsCache.GetData(productID); if (product == null) { // Retrieve it from the data provider // and cache it for more requests. product = dataProvider.GetProductByID(productID); if (product != null) { productsCache.Add(productID, product); } }

Create cache managerCreate cache manager

CacheManager productsCache = CacheManager.GetCacheManager();

Add items when retrieved from data sourceAdd items when retrieved from data source

Proactive caching is recommended in situations Proactive caching is recommended in situations that have one or more of the following that have one or more of the following characteristics:characteristics:

You are using static or semistatic state that has known You are using static or semistatic state that has known update periods. If you use it in other scenarios, the state update periods. If you use it in other scenarios, the state might expire before it is used. might expire before it is used.

You are using state with a known lifetime. You are using state with a known lifetime.

You are using state of a known size. If you use proactive You are using state of a known size. If you use proactive cache data loading when you do not know the size of the cache data loading when you do not know the size of the data, you might exhaust system resources. You must try data, you might exhaust system resources. You must try to not use resources that you do not have. to not use resources that you do not have.

You have problematic resources, such as a slow You have problematic resources, such as a slow database, a slow network, or unreliable Web services. database, a slow network, or unreliable Web services. You can use this technique to retrieve all the state You can use this technique to retrieve all the state proactively, cache it, and work against the cache as proactively, cache it, and work against the cache as much as it can. much as it can.

Loading the CacheLoading the Cache

Reactive caching is recommended in Reactive caching is recommended in situations that have one or more of the situations that have one or more of the following characteristics:following characteristics:

You are using lots of state and you do not have You are using lots of state and you do not have sufficient resources to cache all state for the sufficient resources to cache all state for the entire application. entire application.

You are using reliable and responsive You are using reliable and responsive resources, such as a database, network, or Web resources, such as a database, network, or Web service that will not impede application stability service that will not impede application stability and performance. and performance.

You are interested in caching data that is not You are interested in caching data that is not available during the initialization of an available during the initialization of an application. For example, this data might be application. For example, this data might be affected by user input such as common search affected by user input such as common search queries or user-specific data such as a user's queries or user-specific data such as a user's profile.profile.

Loading the CacheLoading the Cache

Remove item with specified keyRemove item with specified key

Removing an Item from Removing an Item from the Cachethe Cache

productsCache.Remove(“Product101Key”)

No error occurs if key is not foundNo error occurs if key is not found

Flushing the CacheFlushing the CacheUse to manage storage, memory and other Use to manage storage, memory and other resources efficientlyresources efficiently

Explicit flushingExplicit flushingInitiated by application codeInitiated by application code

Removes all items from the cacheRemoves all items from the cache

ScavengingScavengingInitiated by application blockInitiated by application block

Based upon priority and last access timeBased upon priority and last access time

Configuration settings control size of cache and Configuration settings control size of cache and number removednumber removed

productsCache.Flush()

Flushing the Cache with Flushing the Cache with ScavengingScavenging

Scavenging configurationScavenging configurationMaximum elements in cacheMaximum elements in cache

Number of items removed when scavenging occursNumber of items removed when scavenging occurs

Flushing the Cache with Flushing the Cache with ScavengingScavenging

Priority is established when an item is Priority is established when an item is added to the cacheadded to the cache

productsCache.Add("Key1", "Cache Item1", CacheItemPriority.High, new ProductCacheRefreshAction(), expireNotice)

Priority valuesPriority valuesLowLow

NormalNormal

HighHigh

NotRemovableNotRemovable

The scavenging algorithm is not an The scavenging algorithm is not an extension pointextension point

View/Application Share: View/Application Share: DemonstrationDemonstration

[Live Meeting View/Application Share. Use [Live Meeting View/Application Share. Use Live MeetingLive Meeting

> > Edit Slide Properties...Edit Slide Properties... to edit.] to edit.]

Key Extensibility PointsKey Extensibility Points

Custom backing store providerCustom backing store providerImplement IBackingStore (derive from Implement IBackingStore (derive from BaseBackingStore)BaseBackingStore)

Use Configuration Console to select custom providerUse Configuration Console to select custom provider

Custom expiration policyCustom expiration policyImplement ICacheItemExpirationImplement ICacheItemExpiration

Specify object on call to Add method of Specify object on call to Add method of CacheManager objectCacheManager object

Plus…Plus…Anything and everything – you have the source Anything and everything – you have the source code!code!

Please post extensions and suggestions to the Please post extensions and suggestions to the communitycommunity

SecuritySecurity

CryptoCrypto

ConfigurationConfiguration

Data Data AccessAccess LoggingLogging

CachingCaching ExceptionsExceptions

Enterprise Library v1Enterprise Library v1

Legend

Dependency

Plug-in

ConfigConfigToolTool

Announcing: Enterprise Library Announcing: Enterprise Library 1.01.0

http://www.microsoft.com/practicehttp://www.microsoft.com/practicess

Download it

Today!

patterns & practices patterns & practices Live!Live!

3/14 Enterprise Library Logging & 3/14 Enterprise Library Logging & Instrumentation Application Block Instrumentation Application Block

3/17 Enterprise Library Exception Handling 3/17 Enterprise Library Exception Handling Application BlockApplication Block

3/22 Enterprise Library Cryptography 3/22 Enterprise Library Cryptography Application BlockApplication Block

3/24 Enterprise Library Security Application 3/24 Enterprise Library Security Application BlockBlock

http://www.pnplive.com

http://www.microsoft.com/practiceshttp://www.microsoft.com/practices

Enterprise Library CommunityEnterprise Library Communityhttp://http://go.microsoft.com/fwlink/?linkidgo.microsoft.com/fwlink/?linkid=39209&clcid=0x09=39209&clcid=0x09