Caching enhancements

In ColdFusion, in addition to the default caching engine, Ehcache, the following caching engines are supported:

Changes in the Administrator settings

In the ColdFusion administrator, you can choose the caching engine by clicking Server Settings > Caching.

Choose cache engine
Choose cache engine

For each option, you must make some configuration changes.

JCS

Java Caching System (JCS) is an open source caching engine released through the Apache Jakarta sub-project. JCS provides in-memory caching and algorithms for selectively removing objects from the cache. It also offers more advanced features, such as, indexed disk caching and support for distributed caches.

To use JCS, choose the option in the ColdFusion administrator, as shown above.

You can also configure JCS at the application level. Use the new application variables to set the cache engine and the caching properties file.

Application.cfc

component{
  this.name = "appSpecificCacheTest";
  this.cache.configfile = "jcsconfig.properties";
      this.cache.engine = 'jcs';
  this.applicationTimeout = createtimespan(0,0,0,5); 
}

For more information on the variables you can define in Application. cfc , see Application.cfc variables.

jcsconfig.properties

maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5

You can also modify the settings via the Administrator (Server Settings > Caching), as shown below:

JCS properties
JCS properties

To verify the configuration, create a cfm, as shown below:

<cfscript>
 writedump(cacheGetEngineProperties()); // Returns the cache engine properties
 writeoutput(cacheGetEngineProperties().name); // Returns the name of the cache engine
</cfscript>

Auxiliary cache support in JCS

You can use JCS to persist cache into a database, which can be accessed via multiple nodes. From the ColdFusion administrator, add a data source for clustering JCS.

From the ColdFusion Administrator, click Server Settings > Caching. Choose the data source from the drop-down.

JCS auxiliary cache settings
JCS auxiliary cache settings

When you save the changes, a table, JCS_STORE gets created in the selected datasource.

This example uses My SQL as auxiliary cache.

To support auxiliary cache in JCS, edit the file cache.ccf located in <coldfusion_install_dir>/cfusion/lib. Add the following lines:

# MYSQL disk cache used for flight options
jcs.auxiliary.MYSQL=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheFactory
jcs.auxiliary.MYSQL.attributes=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheAttributes
jcs.auxiliary.MYSQL.attributes.userName=<user name>
jcs.auxiliary.MYSQL.attributes.password=<password>
# Make sure the datasource is the one in which JCS_STORE is created 
# via the Administrator
jcs.auxiliary.MYSQL.attributes.url=jdbc:mysql://localhost:3306/<datasource>
jcs.auxiliary.MYSQL.attributes.driverClassName=com.mysql.jdbc.Driver
jcs.auxiliary.MYSQL.attributes.tableName=JCS_STORE
jcs.auxiliary.MYSQL.attributes.UseDiskShrinker=false

Also set jcs.default = MYYSQL in cache.ccf.

Restart ColdFusion.

The JDBC disk cache uses a relational database, such as MySQL, as a persistent data store. The cache elements are serialized and written into a BLOB.

Memcached

Memcached is a distributed caching solution for Java enterprise applications. A Memcached server can be shared across multiple ColdFusion instances, or multiple Memcached servers can be configured with a single ColdFusion instance.

To use Memcached, download Memcached, and run the server from the command line using the command:

memcached

The Memcached server, picks up the default port 11211.

In the ColdFusion administrator, specify the Memcached server details in Server Settings > Caching.

Memcached server
Memcached server

You can also configure Memcached at the application level. There are two new application variables that you need to use to declare the caching engine.

  • this.cache.engine: Specify the caching engine to be used. Choose from jcs or memcached or ehcache .
  • this.cache.configFile: Specify the configuration file for memcached . For example, the file can contain key-value pairs of caching properties, as shown below:
maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5

You can also modify the settings via the Administrator (Server Settings > Caching), as shown below. The interface is similar for other cache engines:

Memcached settings
Memcached settings

// Application.cfc

component{
 this.name='appUsingMemcached';
 this.cache.engine='memcached';
 this.cache.configFile='memcachedconfig.properties';
 this.cache.applicationTimeout=createtimespan(0,0,0,5);
}
// memcachedconfig.properties

maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=10

Redis

Redis is an open source (BSD licensed), in-memory data structure store, used as a database and cache. It supports data structures such as strings, hashes, lists, sets, and so on.

To use Redis, download Redis, and run the server from the command line using the command:

redis-server

The Redis server picks up the default port 6379.

Redis is distributed. In a clustered environment, all nodes can communicate with the same Redis node.

In the ColdFusion administrator, specify the Redis server details in Server Settings > Caching.

Redis settings
Redis settings

You can also configure Redis at the application level. There are two new application variables that you need to use to declare the caching engine.

  • this.cache.engine: Specify the caching engine to be used. Choose from jcs, redis, memcached or ehcache.
  • this.cache.configFile: Specify the configuration file for redis. For example, the file can contain key-value pairs of caching properties, as shown below:
maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5
<!--- Application.cfc --->

<cfcomponent>
    <cfscript>
        this.name = "appSpecificCacheTest";
        this.cache.configfile = "redisconfig.properties";
        this.cache.engine = "redis";
        this.applicationTimeout = createtimespan(0,0,0,5);
    </cfscript>
</cfcomponent>
// redisconfig.properties

maxElementsInMemory=5
eternal=false
timeToIdleSeconds=30
timeToLiveSeconds=5

Custom cache plugin

Apart from using Memcached, Redis, or JCS, you can implement your custom cache plugin. We have provided an interface (C:\ColdFusion2021\cfusion\wwwroot\CFIDE\cache\ICustomCache.cfc), through which you can implement a custom caching engine.

Place your implementations in the same folder as ICustomCache.cfc.

ColdFusion takes advantage of Infinispan libraries. Download the files from the stable release,

  • infinispan -embedded-query-9.1.3.Final.jar
  • infinispan -embedded-9.1.3.Final.jar

Copy the files in C:\ColdFusion2021\cfusion\lib and restart ColdFusion.

To add a custom plugin:

  1. Create a folder in C:\ColdFusion2021\cfusion\wwwroot\CFIDE\cache. Make sure that the name of the folder is same as the name of the custom plugin.

  2. Create <any CFC name>.cfc that implements ICustomCache.cfc and write the implementations. For example,

    <cfcomponent implements="ICustomCache">
        <cffunction name="put" >
            <cfargument name="obj" type="struct">
                <cfoutput>"inside put"</cfoutput>
                <cfset defaultCache=Application.defaultCacheManager.getcache()>
                <cfset defaultCache.put(obj.id,#obj.value#)>
        </cffunction>
    </cfcomponent>
  3. Create config.xml that contains references to Infinispan libraries.

    <infinispan>
        <cache-container default-cache="local">
            <local-cache name="local"/>
        </cache-container>
    </infinispan>
  4. Create Application.cfc.

    <cfcomponent> 
        <cfset this.name= "xyz"> 
            <cfscript> 
                function onApplicationStart() 
                { 
                    writelog("In onApplicationStart()"); 
                    Application.defaultCacheManager=CreateObject("java","org.infinispan.manager.DefaultCacheManager").init('C:\ColdFusion2021\cfusion\wwwroot\custom_cache\config.xml'); 
                    writelog("In onApplicationStart()"); 
    } 
                    function onApplicationEnd() { 
                    writelog("In onApplicationEnd()"); 
    } 
            </cfscript> 
    </cfcomponent>

Create an app with the following:

// Application.cfc

<cfcomponent>
    <cfscript>
        this.name = "mycache_app";
        this.cache.engine = "mycache";
    </cfscript>
</cfcomponent>

Admin APIs

The following are the Admin APIs related to caching that are a part of the 2018 release of ColdFusion. The APIs are a part of runtime.cfc.

verifyRedisCacheStorageConnection

Description: Verifies connection to the Redis cache storage.

Syntax

void verifyRedisCacheStorageConnection (sessionStorageHost, numeric sessionStoragePort, sessionStoragePassword)

Parameter

Req/Opt

Default

Description

sessionStorageHost

Optional

Any

The hostname for Redis cache storage.

sessionStoragePort

Optional

Numeric

The port number for Redis cache storage.

sessionStoragePassword

Optional

Any

The password for the Redis cache storage.

setServerCachingEngine

Description: Changes the caching engine at the server level.

Syntax

void setServerCachingEngine (required engine)

Parameter

Req/Opt

Default

Description

engine

Required

Any

1.       Ehcache

2.      JCS

3.      Memcached

4.      Redis

setJCSClusterDsnName

Description: Set the data source for JCS cluster.

Syntax

void setJCSClusterDsnName (required dsn, required boolean createTables)

Parameter

Req/Opt

Default

Description

dsn

Required

Any

Name of the data source.

createTables

Required

Any

Whether to create a table.

setCachingRedisServer

Description: Set the caching engine for Redis.

Syntax

void setCachingRedisServer (required host, required port, required password, required boolean cluster)

Parameter

Req/Opt

Default

Description

host

Required

any

Host address of the server.

port

Required

any

Port number of the server.

password

Required

any

Password of the server.

cluster

Required

Boolean

Whether a cluster is enabled in Redis.

getMemcachedServer

Description: Gets the details of the Memcached caching engine.

Syntax

any getMemcachedServer ()

For more information on caching in ColdFusion, see Optimizing ColdFusion applications.

Azure Redis Cache

Azure Cache for Redis provides an in-memory data store based on Redis. The caching performance is enhanced by copying frequently accessed data to a high performant storage. With Azure Cache for Redis, this storage is located in-memory instead of being loaded from disk by a database.

Azure Cache for Redis can be used as the following:

  • Distributed data cache
  • Session store
  • Message broker

Azure Cache for Redis is hosted on Azure, and accessible to any application within or outside of Azure. For more information, see Azure Cache for Redis.

In the Azure portal, create a Redis cache.

In the cache details page, enter the following:

  • Primary key
  • Secondary key
  • Primary connection string
  • Secondary connection string

Use these connection details in the Caching page of the ColdFusion Administrator.

After you click Verify Connection, the caching engine now gets stored in Azure cloud.

AWS ElastiCache

Amazon ElastiCache is a Caching-as-a-Service from Amazon Web Services. AWS manages and scales a distributed in-memory cache environment in the cloud. AWS removes the complexity associated with deploying and managing a distributed cache environment.

For more information, see AWS ElastiCache.

AWS ElastiCache provides the following:

  • Automatic detection and recovery from cache node failures.
  • Automatic failover of a failed primary cluster.
  • Flexible Availability Zone placement of nodes and clusters.

Amazon ElastiCache provides two caching engines, Memcached and Redis. You can move your existing Memcached or Redis caching implementation to Amazon ElastiCache effortlessly. Simply change the Memcached/Redis endpoints in your application.

For AWS, you must create an SSL tunnel to an EC2 instance. It will then redirect your localhost to the caching engine port in AWS.

In the AWS console, for Redis and Memcached, create the cache, and then enter the details in the Caching details page of the ColdFusion Administrator.

 Adobe

Get help faster and easier

New user?