Accelerate your ColdFusion Applications using Caching 
S V PAVAN KUMAR 
sanniset@adobe.com 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
October 2014
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 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
During experiments, Many bird 
species store peanuts in a cache for 
later retrieval. In the wild, these birds 
store acorns and insects. 
-Wikipedia
Cache 
A Simple Key/Value Pair 
Key Value 
USA Washington D.C 
India New Delhi 
England London 
Japan Tokyo 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
How Cache Works ? 
Key Value 
Key1 Value1 
Key2 Value2 
Key3 Value3 
Key4 Value4 
Application 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
Cache 
Hit 
Database Network Computation
How Cache Works ? 
Miss 
Key Value 
Key1 Value1 
Key2 Value2 
Key3 Value3 
Key4 Value4 
Application 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
Cache 
Database Network Computation 
Cache - 
Aside
How Cache Works ? 
Miss Key Value 
Key1 Value1 
Key2 Value2 
Key3 Value3 
Key4 Value4 
Application 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
Cache 
Database Network Computation 
Read - 
Through 
Sync/ Async
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 
Application level Cache Manager Server Level Cache Manager 
QUERY 
TEMPLATE 
Application level Cache 
Manager 
Application level Cache Manager 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
OBJECT 
QUERY 
TEMPLATE 
OBJECT 
QUERY 
TEMPLATE 
OBJECT
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() 
 CacheRegionRemove( 
) 
 CacheGetProperties() 
 CacheSetProperties() 
• CacheGet() 
• CacheGetAllIds() 
• CachePut() 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
• CacheRemove() 
• CacheRemoveAll()
Cache Metadata 
 CacheGetMetadata(): to get cache meta data for cache entry 
cache_hitcount Number of hits 
cache_misscount Number of misses 
createdtime Creation time 
Name Cache region Name 
idletime time to idle for an element before it expires 
lasthit last hit timestamp 
Lastupdated last modified timestamp 
size Cached Object size in bytes 
timespan 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 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
Database 
ColdFusion 
Application 
Session Level Cache 
Secondary Cache 
(Ehcache)
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 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
 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 
Naviga 
tion 
Bar 
Content 
Footer 
<cfcache>
Template Cache: Fragments 
</cfcache> </cfcache> 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
 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 
Naviga 
tion 
Bar 
Content 
Footer 
<cfcache> 
</cfcache> 
<cfcache> <cfcache> 
<cfcache> 
</cfcache>
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 
CacheI 
d Given 
? 
No Yes 
Get the 
Request URL 
Yes 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
useQuer 
yString ? 
No 
Append Query 
String from 
URL 
Compute a hash 
from page physical 
path and Append 
Has 
End 
Tag? 
Append Line No 
No 
depend 
s on ? 
Append 
dependent 
variables 
Yes 
Template Cache 
Key 
No 
Yes 
http://localhost/example/index.cfm_query_themeid=1_pageid:17CF7BD640264F4CB3D507A4 
72E0190Bsession.username=JZNKA
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 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
ColdFusio 
n 
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
Replicated 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
ColdFusio 
n 
Cach 
JVeM 
Cach 
JVeM 
Cach 
JVeM 
Cach 
JVeM 
RMI, Jgroup 
Protocols 
Put, Remove, 
Remove All, 
Bootstrapping (Sync 
or Async) 
ColdFusio 
n 
ColdFusio 
n 
ColdFusio 
n 
• Pros: 
• In Process 
• Data Consistency in 
Cluster 
• Bootstrapping from a 
peer node 
• Cons: 
• GC Pauses 
• Serialization overhead 
• Limited by CF JVM 
Memory
Centralized 
ColdFusion 
Cache 
(L1) 
JVM 
ColdFusion 
Cache 
(L1) 
JVM 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
Big 
Memory 
(L2 
Cache) 
JVM
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
Demo
<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 
<cacheManagerPeerProviderFactory 
class="net.sf.ehcache.distribution.RMICacheManagerPeerProvider 
Factory" 
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.RMICacheManagerPeerProvid 
erFactory" 
properties="peerDiscovery=manual,rmiUrls=//server2:40001/sam 
pleCache11|//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.RMICacheManagerPeerListen 
erFactory" 
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.RMIBootstrapCacheLoaderFa 
ctory" 
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.ProbePeerListe 
nerFactory" 
properties="monitorAddress=localhost, monitorPort=9889, 
memoryMeasurement=true" /> 
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 
Q & A
© 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Accelerate your ColdFusion Applications using Caching

  • 1.
    Accelerate your ColdFusionApplications using Caching S V PAVAN KUMAR sanniset@adobe.com © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. October 2014
  • 2.
    Agenda  CachingOverview  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.
  • 3.
    Cache © 2014Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. During experiments, Many bird species store peanuts in a cache for later retrieval. In the wild, these birds store acorns and insects. -Wikipedia
  • 4.
    Cache A SimpleKey/Value Pair Key Value USA Washington D.C India New Delhi England London Japan Tokyo © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 5.
    How Cache Works? Key Value Key1 Value1 Key2 Value2 Key3 Value3 Key4 Value4 Application © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Cache Hit Database Network Computation
  • 6.
    How Cache Works? Miss Key Value Key1 Value1 Key2 Value2 Key3 Value3 Key4 Value4 Application © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Cache Database Network Computation Cache - Aside
  • 7.
    How Cache Works? Miss Key Value Key1 Value1 Key2 Value2 Key3 Value3 Key4 Value4 Application © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Cache Database Network Computation Read - Through Sync/ Async
  • 8.
    Simply Sacrifice memoryfor latency reduction © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 9.
    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.
  • 10.
    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.
  • 11.
    Cache Efficiency Cachesare not a panacea. © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 12.
    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.
  • 13.
    Overview  IndustryStandard 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.
  • 14.
    CF Cache layout Application level Cache Manager Server Level Cache Manager QUERY TEMPLATE Application level Cache Manager Application level Cache Manager © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. OBJECT QUERY TEMPLATE OBJECT QUERY TEMPLATE OBJECT
  • 15.
    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.
  • 16.
    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.
  • 17.
    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.
  • 18.
    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.
  • 19.
    Cache Regions: CRUD  CacheRegionNew()  CacheRegionExists()  CacheRegionRemove( )  CacheGetProperties()  CacheSetProperties() • CacheGet() • CacheGetAllIds() • CachePut() © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. • CacheRemove() • CacheRemoveAll()
  • 20.
    Cache Metadata CacheGetMetadata(): to get cache meta data for cache entry cache_hitcount Number of hits cache_misscount Number of misses createdtime Creation time Name Cache region Name idletime time to idle for an element before it expires lasthit last hit timestamp Lastupdated last modified timestamp size Cached Object size in bytes timespan time to live for an element before it expires. © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 21.
    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.
  • 22.
    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.
  • 23.
    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.
  • 24.
    ORM Session LevelCache  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.
  • 25.
    ORM Second levelCaching  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.
  • 26.
    ORM Cache Hierarchy © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Database ColdFusion Application Session Level Cache Secondary Cache (Ehcache)
  • 27.
    ORM Second levelCaching 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.
  • 28.
    ORM Second levelCaching 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.
  • 29.
    ORM Second levelCaching 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.
  • 30.
    ORM Second levelCaching 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.
  • 31.
    ORM Cache AccessStrategies  Read Only  Nonstrict-read-write  Read-write © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 32.
    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.
  • 33.
    Template Cache: Pages © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.  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 Naviga tion Bar Content Footer <cfcache>
  • 34.
    Template Cache: Fragments </cfcache> </cfcache> © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.  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 Naviga tion Bar Content Footer <cfcache> </cfcache> <cfcache> <cfcache> <cfcache> </cfcache>
  • 35.
    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.
  • 36.
    Template Cache Key CacheI d Given ? No Yes Get the Request URL Yes © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. useQuer yString ? No Append Query String from URL Compute a hash from page physical path and Append Has End Tag? Append Line No No depend s on ? Append dependent variables Yes Template Cache Key No Yes http://localhost/example/index.cfm_query_themeid=1_pageid:17CF7BD640264F4CB3D507A4 72E0190Bsession.username=JZNKA
  • 37.
    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.
  • 38.
    Distributed Caching ©2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 39.
    Standalone  Pros:  In Process & faster access  No serialization of keys and values  Eventual Consistency local to Application © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. ColdFusio n 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
  • 40.
    Replicated © 2014Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. ColdFusio n Cach JVeM Cach JVeM Cach JVeM Cach JVeM RMI, Jgroup Protocols Put, Remove, Remove All, Bootstrapping (Sync or Async) ColdFusio n ColdFusio n ColdFusio n • Pros: • In Process • Data Consistency in Cluster • Bootstrapping from a peer node • Cons: • GC Pauses • Serialization overhead • Limited by CF JVM Memory
  • 41.
    Centralized ColdFusion Cache (L1) JVM ColdFusion Cache (L1) JVM © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Big Memory (L2 Cache) JVM
  • 42.
    © 2014 AdobeSystems Incorporated. All Rights Reserved. Adobe Confidential. Demo
  • 43.
    <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.
  • 44.
    Peer Node Discovery  Automatic Peer Discovery <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProvider Factory" 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.
  • 45.
    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.RMICacheManagerPeerProvid erFactory" properties="peerDiscovery=manual,rmiUrls=//server2:40001/sam pleCache11|//server2:40001/sampleCache12"/> © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 46.
    Peer to PeerCommunication Configure hostname & port to listen for messages from peers <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListen erFactory" properties="hostName=localhost, port=40001, socketTimeoutMillis=2000"/> *Port must be unique to each peer © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 47.
    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.
  • 48.
    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.
  • 49.
    Peer Bootstrapping Enable Bootstrapping from peer for auth cache <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFa ctory" properties="bootstrapAsynchronously=false, maximumChunkSizeBytes=5000000" propertySeparator="," /> © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 50.
    Monitoring  Copyehcache-probe-<version>.jar to lib directory of each peer  Add the below listener to monitor the cache <cacheManagerPeerListenerFactory class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListe nerFactory" properties="monitorAddress=localhost, monitorPort=9889, memoryMeasurement=true" /> © 2014 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
  • 51.
    © 2014 AdobeSystems Incorporated. All Rights Reserved. Adobe Confidential. Q & A
  • 52.
    © 2014 AdobeSystems Incorporated. All Rights Reserved. Adobe Confidential.