In the 2018 release of 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:\ColdFusion2018\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:\ColdFusion2018\cfusion\lib and restart ColdFusion.

To add a custom plugin:

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

  2. Create cache.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:\ColdFusion2018\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.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy