CFML
Use Redis from CFML natively
The first and easiest way to use your new cache is via the built-in cache functions and tags in CFML. You can specify the cache name to use, but by default Lucee will use the cache you have designated as your default object
cache.
Caching Objects
If you have selected your cache as the default Object
cache in the admin, you can simply use functions like cachePut()
and cacheGet()
. Redis can handle arbitrary snippets of text (like HTML snippets) and even complex objects such as queries, arrays, or structs. The cache will automatically serialize the values and store them as text. They will be reconstituted when you retrieve them from the cache later.
Caching Function Output
Once you have selected a cache as the default function
cache in the admin, you can use Redis to cache the results of oft-run functions. The cache key will be created for you automatically based on a hash of the method arguments thanks to Lucee. Cached functions should therefore be deterministic-- meaning the output of the function is purely a product of its parameters. Using function caching is easy, just add a cachedwithin
attribute to the cffunction
tag or function declaration.
This is what that function would look like in script
Important: Lucee only supports caching functions that accept simple values as parameters. Therefore, a function that accepts an array would not be cacheable.
Caching Queries
Once you have selected a cache as the default query
cache in the admin, you can use Redis to cache the results of oft-run database queries. The cache key will be created for you automatically based on a hash of the SQL statement and its parameters thanks to Lucee. Using query caching is as easy as function caching, just add a cachedwithin attribute to your query.
Per-application Cache Settings
You can specify what caches are the default storage mechanism for functions, queries, objects, etc in the Lucee server and web administrator. There is one more programmatic level this can be configured as well.
The following settings are available in your Application.cfc
to override at an application level. Remember, the "object" cache is used by default if no cache name is specified to functions such as cachePut()
and cacheGet()
.
You can also define an entire cache in your Application.cfc
which makes your configuration completely portable.
Last updated