Accelerate your ColdFusion Applications using Caching
-
Upload
venkata-pavan-kumar-sannisetty -
Category
Software
-
view
54 -
download
4
Transcript of Accelerate your ColdFusion Applications using Caching
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Accelerate your ColdFusion Applications using CachingS V PAVAN [email protected]
October 2014
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Agenda
Caching Overview CF Ehcache Implementation
CF Cache layout Application specific caching Query Caching ORM Caching Template & Object Caching
Distributed Caching <cflogin> cache replication demo
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Cache
During experiments, Many bird species store peanuts in a cache for later retrieval. In the wild, these birds store acorns and insects.
-Wikipedia
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Cache
A Simple Key/Value Pair
Key ValueUSA Washington D.CIndia New DelhiEngland LondonJapan Tokyo
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
How Cache Works ?
Key ValueKey1 Value1Key2 Value2Key3 Value3Key4 Value4
Application
CacheHit
Database ComputationNetwork
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
How Cache Works ?
MissKey ValueKey1 Value1Key2 Value2Key3 Value3Key4 Value4
Application
Cache
Database ComputationNetwork
Cache - Aside
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
How Cache Works ?
Miss Key ValueKey1 Value1Key2 Value2Key3 Value3Key4 Value4
Application
Cache
Database ComputationNetwork
Read - Through
Sync/ Async
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Simply
Sacrifice memory for latency reduction
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Eviction Strategies
Manually deleting Expiration
Expire after 2 Hours Expire at Oct 18th, 2014
When Cache memory is Full Delete using FIFO | LRU | LFU Spill to Disk
EhCache does not remove expired elements immediately from memory.
i
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Cache Bootstrapping
How to pre-load the cache on startup From Disk store From a peer cache node From a central Cache server (e.g. Terracotta Server) Start Empty, fill later (Cold Cache)
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Cache Efficiency
Caches are not a panacea.
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ColdFusion Caches
Query Cache ORM Cache
Session level 2nd Level Cache
Template Page Fragment
Object Cache <Cflogin> Cache
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Overview
Industry Standard Caching Provider Default cache provider for ColdFusion since CF 9 Various cache algorithms FIFO, LRU and LFU Better control over cache through config Disk persistence Cache replication Distributed caching support Monitoring Thread Safe
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
CF Cache layout
Server Level Cache ManagerApplication level Cache Manager
Application level CacheManager
Application level Cache Manager
QUERY TEMPLATEOBJECT
QUERY
TEMPLATE
OBJECT
QUERYTEMPLATE
OBJECT
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Application Specific Caching
Added in CF 10 If enabled, caching is done at application level Set this.cache.configfile in Application.cfc
Absolute: this.cache.configfile = "c:/myApp/ehcache.xml" Relative to Application.cfc: this.cache.configfile =
"ehcache.xml" Server level Ehcache configuration file ehcache.xml can be
found at <Install_location>\<Instance name>\lib
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Default Cache Regions
TEMPLATE OBJECT QUERY
Default cache names will be prefixed with applicationName (when app specific caching is not enabled)
<App Name>TEMPLATE <App Name>OBJECT <App Name>QUERY
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Custom Cache Regions
Using ehcache.xml in <CF Instance Name>/lib (Hard Coding) cacheRegionNew() to create on-fly cache regions All the caches supporting cacheRegion All the cache functions supporting region argument Custom cache region can contain any type of Data
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Cache Region Properties
Cache region properties retrieved using cacheGetProperties() set using cacheSetProperties()
<cache
name=“myCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="720"
timeToLiveSeconds="720"
overflowToDisk="true"
maxElementsOnDisk="100000"
diskPersistent="true"
memoryStoreEvictionPolicy="LRU"
/>
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Cache Regions: CRUD
CacheRegionNew() CacheRegionExists(
) CacheRegionRemov
e() CacheGetProperties
()
CacheSetProperties()
• CacheGet()• CacheGetAllIds()• CachePut()
• CacheRemove()• CacheRemoveAll()
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Cache Metadata
CacheGetMetadata(): to get cache meta data for cache entry
cache_hitcount Number of hitscache_misscount Number of missescreatedtime Creation timeName Cache region Nameidletime time to idle for an element before it expireslasthit last hit timestampLastupdated last modified timestampsize Cached Object size in bytestimespan time to live for an element before it expires.
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Query Cache - cachedwithin
<cfquery
name="GetParks" datasource="cfdocexamples"
cachedwithin="#CreateTimeSpan(0, 6, 0, 0)#“ cacheRegion=“statewideParks”> SELECT PARKNAME, REGION, STATE FROM Parks
ORDER BY ParkName, State
</cfquery>
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Query Cache
Backed by Ehcache by default from CF10. Internal cache fallback
Server level setting Caching -> Use internal cache to store queries Application level specify
this.cache.useinternalquerycache=true|false Query limit
Server level setting Caching -> Maximum number of cached queries default 100
Application level specify this.cache.querysize = 150
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Query Cache
Datasource name, the actual SQL statement itself, and the parameters passed to the SQL statement will be used to pull out from the cache if cache id not defined
dbtype=query" and "CachedWithin" are mutually Exclusive
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ORM Session Level Cache
Entities loaded from DB cached in ORM session Short living exists as long as session is open Caches persistent Objects EntityLoad function
For the first time hits DB gets the entity. For the second time retrieves from ORM session Should use EntityReload to force retrieval from DB
ORMClearSession() clears session level cache
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ORM Second level Caching
CF uses EHCache as the default secondary cache provider from CF 9
Caches Persistent Object Persistent Object Associations Results of Queries
Long lived Supports Application specific Cache
Absolute: this.ormsettings.cacheConfig = "c:/myApp/ehcache.xml" Relative to Application.cfc: this.ormsettings.cacheConfig =
"ehcache.xml" Can Scale in clustered environment
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ORM Cache Hierarchy
Database
ColdFusion Application
Session Level Cache
Secondary Cache (Ehcache)
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ORM Second level Caching Example
Application.cfc<cfset this.name="Caching_Example">
<cfset this.datasource="cfartgallery">
<cfset this.ormenabled="true">
<cfset this.ormsettings.secondarycacheEnabled=true>
<cfset this.ormsettings.cacheProvider= "ehcache">
<cfset this.ormsettings.cacheConfig="ehcache.xml">
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ORM Second level Caching Example
CArtist.cfc
<cfcomponent persistent="true" schema="APP" table="Artists" cachename="artist" cacheuse="read-only">
<cfproperty name="artistid" fieldtype="id"/>
<cfproperty name="firstname"/>
<cfproperty name="lastname"/>
<cfproperty name="state"/>
<cfproperty name="art" fieldtype="one-to-many" cfc="CArt" fkcolumn="ArtID" cachename="ArtistArts" cacheuse="read-only">
</cfcomponent>
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ORM Second level Caching Example
CArt.cfc
<cfcomponent persistent="true" schema="APP" table="Art">
<cfproperty name="artid" generator="identity" fieldtype="id"/>
<cfproperty name="artname"/>
<cfproperty name="issold"/>
</cfcomponent>
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ORM Second level Caching Example
<cfscript>
//This will cache the Artist Component and also the association. It wouldn't cache the Art objects.
artistObj = EntityLoad("CArtists", 3, true);
//This will cache the query.
availableArts = ORMExecuteQuery("from CArt where issold=0", {}, false, {cacheable=true, cachename="availableArtsCache"});
</cfscript>
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ORM Cache Access Strategies
Read Only Nonstrict-read-write Read-write
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
ORM Cache: Eviction
ORMEvictEntity("<entity_name>", [primarykey]) ORMevictcollection("<entity_name>", "<association_name>",
[primarykey]) ORMEvictQueries(cachename, datasource)
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Template Cache: Pages
Both server- side and client-side caching
Cache an entire page Single <cfcache> at top
of page No need of closing tag
</cfcache> Also, <cfcache/>
Header
Navigation Bar
Content
Footer
<cfcache>
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Template Cache: Fragments
Cache individual fragments of content
Useful when parts of a page must remain dynamic or personalized.
Surround with <cfcache>…</cfcache> tags
Multiple fragments can be cached in a single request
Each fragment can be configured independently
No Client side caching
Header
Navigation Bar
Content
Footer
<cfcache></cfcache>
<cfcache> <cfcache>
<cfcache>
</cfcache> </cfcache>
</cfcache>
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Template Cache
Timespan The interval until the item is flushed from the cache
idleTime Flushes the cached item if it is not accessed for the specified time span
Metadata Contains cache meta data info such as hit count, miss count created time
etc. expireURL
expireURL along with action = ‘flush’ used to flush pages that match the specified URL or pattern
Wild card Support <cfcache action="flush" expireurl="*.cfm?productID=#URL.productID#">
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Template Cache Key
CacheId
Given ?
Get the Request URL
useQueryString
?No
Append Query String
from URL
Compute a hash from page
physical path and Append
YesNo
Has End Tag?
Append Line No
Yes
depends on ?
No
Append dependent variables
Yes Template Cache Key
No
Yes
http://localhost/example/index.cfm_query_themeid=1_pageid:17CF7BD640264F4CB3D507A472E0190Bsession.username=JZNKA
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Object Cache
Application code can take advantage of caching Can cache anything (Simple values, Complex Objects like files,
images etc.) Cache functions for CRUD operations and to get cache
properties (cfcache) Cache keys need to be managed manually. (id is mandatory)
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Distributed Caching
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Standalone
Pros: In Process & faster access No serialization of keys and values Eventual Consistency local to Application
ColdFusion
Cache
JVM
Cons Not Scalable Limited by CF JVM memory GC pauses On 32 Bit huge caches will suffer Bootstrapping only from Disk Store. Inconsistency on a cluster
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Replicated
ColdFusion
CacheJVM
CacheJVM
CacheJVM
CacheJVM
Put, Remove, Remove All, Bootstrapping (Sync or Async)
RMI, Jgroup Protocols
ColdFusion
ColdFusion
ColdFusion
• Pros:• In Process• Data Consistency in
Cluster• Bootstrapping from a
peer node
• Cons:• GC Pauses• Serialization overhead • Limited by CF JVM
Memory
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Centralized
ColdFusion
Cache (L1)JVM
ColdFusion
Cache (L1)JVM
Big Memory (L2 Cache)
JVM
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Demo
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
<cflogin> Auth cache
Prior to CF9 requires sticky sessions for clustered environment if loginStorage=cookie
CF10 onwards auth info is stored in ehcache To enable long lived logins or remember me type of functionality To provide fail-over and replication support in cluster
Configuration for cflogin cache can be found at <cf-home>/lib/auth-ehcache.xml
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Peer Node Discovery
Automatic Peer Discovery
<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties ="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,multicastGroupPort=4446, timeToLive=32"/>
Automatic addition & deletion of peer nodes
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Peer Node Discovery
Manual Discovery Peers cannot be added or removed at runtime Multicast is not supported between the nodes Each peer should know about all other peers
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//server2:40001/sampleCache11|//server2:40001/sampleCache12"/>
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Peer to Peer Communication
Configure hostname & port to listen for messages from peers
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=40001,
socketTimeoutMillis=2000"/>
*Port must be unique to each peer
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Auth Cache
Configure replication to auth cache<cache name="authcache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="3600"
memoryStoreEvictionPolicy="LRU"
clearOnFlush="true">
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Cache Replication
Enable replication for auth cache
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,
replicateUpdatesViaCopy=false, replicateRemovals=true "/>
</cache>
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Peer Bootstrapping
Enable Bootstrapping from peer for auth cache
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=false, maximumChunkSizeBytes=5000000"
propertySeparator="," />
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Monitoring
Copy ehcache-probe-<version>.jar to lib directory of each peer Add the below listener to monitor the cache
<cacheManagerPeerListenerFactory class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"
properties="monitorAddress=localhost, monitorPort=9889, memoryMeasurement=true" />
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Q & A
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.