Cache (Advanced) Part 2

January 26, 2010 · By Michael Offner · 1 Comment

In the first part of the cache blog entry we looked at how we can use the cache directly without entering into great detail. Now let's have a look at what the cache is at capable of. Because it provides much more than just storing data.

Cache Types

Similar to databases, Railo supports different cache types. Imagine you could only use MySQL in Railo and no other database. Now this would be a huge restriction. We believe that the same is true for caches and therefore we have not limited Railo to one single cache type, but left the interface open for several cache types you can use depending on requirements.
At the moment Railo supports the following caches:
  • RamCache
    This cache is shipped with Railo and is based in memory. The cache is very fast and well suited for small applications, but very quickly pushes its limits.
  • EHCache Lite (http://ehcache.org/)
    This cache will be packaged with Railo version 3.2 (unfortunately it is not possible to install it via the update process) and can be installed free of charge via the Extension Manager. This cache is also used in other CFML engines in the same way and it provides a variety of ways to define for how long objects should live and where and when they are stored.
  • EHCache (http://ehcache.org/)
    This cache works similar to "EHCache Lite", but in addition it allows the configuration of a cluster by connecting to other EHCache servers via a peer-2-peer cluster.
  • Memcached (http://memcached.org/)
    This cache is offered as a free extension and works in the same way "EHCache Lite" does. Just like "EHCache" this cache provides a cluster solution, but not as peer-2-peer model, as client-server model. The data is not stored locally but on a centralized server, similar to a database.
Other types of cache will follow, and of course you can write a cache driver yourself. If you are you interested in doing so, just contact us, we are happy to help.

Install EHCache Lite

As I have written above "EHCache Lite" is available as an extension and must be installed first. This is really very simple. Just go to the Railo Server Administrator and select the menu link Extension / Applications (/ railo-context/admin/server.cfm?action=extension.applications).

Under "not installed" you will find a list of available extensions (they are not installed). Check the radio button for "EHCache Lite" and click install.

Now you should see a confirmation about the successful installation. Click "OK".

You will return to the overview and should now see "EHCache Lite" in the list of installed applications.

Managing Caches

Change now to the Web Admin and select the menu "Services / Cache (Beta)" (/railo-context/admin/web.cfm?Action=services.cache) In the first part of the blog we already created a "RamCache" cache called "mycache", which is defined as the default object cache.

In the drop down under "Create a new cache connection" you should find the newly installed "EHCache Lite". Select it and give it the name "ehquery" and click on "create."

You see, "EHCache Lite" offers much more possibilities than the configuration of the "RamCache". I'm not going to describe each option as they are described in the subtext. If something is not clear, do not hesitate to ask us. I leave the configuration as is and select "Query" at the "Default" dropdown and click "submit". Now you go back to the overview and add two more caches called "ehtemplate" and "ehresource" the same way as "ehquery". Please select them as the default for Template and Resource.
Now the list should look something like this:

You have created all possible default caches available in Railo at the moment, now what does this mean? As mentioned the cache can not only be used as a Object storage using the cache tags and functions, but it can be used as an automatic storage for other services.

Template Cache
The tag cfcache has the actions "flush, get, put" in order to read and write Objects, similar to the functions cacheGet, CachePut, CacheClear. Historically the tag however offers another possibility. You can store entire pages or parts of them. In earlier versions these elements were stored in the file system. Now this data can be stored in a cache by using a "Default Template" cache, a definition we have made with the cache "ehtemplate".

Query Cache
When you use the cfquery tag attributes "cachedwithin" and "cachedafter" you can define a timespan of how long a query will be helt in memory, which can be very memory consuming. Now it is also possible to store this data in a cache simply by (you probably already guess it) defining a "Default Query" cache, something what we have done with the cache "ehquery.

Resource cache (as of Version 3.1.2.002)
As you may already know Railo has a virtual file system called "ram ://", that allows you to store files in the memory of the server. Example:
<cffile action="write" file="ram://susi.txt" output="Hello Susi"> This functionality is limited, because the memory itself is a precious resource and that the contents of it will be flushed if a restart of the system is done. You can use a cache here as well since you get many more possibilities of persisting the data. First of all you just have have to define the "Default Resource" for it. This is what we did with the "ehresource". I have to mention that for instance EHCache stores of the data first in memory and persits it to disc asynchronously, so that the resource is not slower than a pure RAM resource. The Cache based RAM Resource is not available by default, and has to be configured specially. If someone is interested on how to do it I can explain it in a comment.

Now to summarize, we have learned that Railo does not only support one but several (four at the time) different cache types. In addition you can define as many cache instances as you want and configure each of them differently. As usual, this is possible for each web context individually. In Railo caches are not only used for storing application data, but as a backend for various services as well. Railo stores templates, queries and ram files in a cache. The reason for this is that Railo does not see the cache as an extension, but as an integral part of the engine.

In the third and final part we will look at some specialities and list the reference to the current implementation of the tags and function.

Tags: cache · CFML · Configuration · Extension · Features · Railo 3.1.2

1 response so far ↓

  • 1 John Whish // Aug 9, 2014 at 1:59 PM

    Did you ever write part 3? :)

Leave a Comment

Leave this field empty: