CFML
Use Couchbase 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()
. Couchbase 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 retreive them from the cache later.
Important: A single cache item can be up to 20 MB in size!
If you pass in complex objects to cachePut()
, they will be serialized as binary and not usable in Couchbase views.
Caching Function Output
Once you have selected a cache as the default function cache in the admin, you can use Couchbase 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
<strong>
{=html}Important</strong>
{=html}: Lucee only supports caching functions that accept simple values as paramters. 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 Couchbase 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.
Lucee RAM Resources
Lucee has the concept and feature of virtual file systems. Our extension taps into it by allowing you to leverage the ram://
resource to talk to Couchbase for storing documents and treating Couchbase like a big file system. This means that any file or directory related tag/function can work with Couchbase RAM resource like: fileRead(), fileWrite(), fileDelete(), directoryNew(), directoryList(), include
, etc. It even works when defining application mappings. You can define Couchbase to be the default cache for resources by selecting the default element in the cache details page and pointing it to resource.
Important: Please note that Lucee only allows you to determine 1 cache connection to be the default for either object, template, query, resource or function caching. So you might end up creating different cache connections if you want to leverage them as default cache locations.
Once you setup Couchbase to be your default resource cache you can leverage it in many ways:
Per-application Cache Settings
You can speicify 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