Version 2 (modified by boomfish, 16 years ago)

Changed spelling of 'adaptor' to 'adapter' so as to match the code.

Content Caching

Model-Glue has historically provided some basic caching features. Model-Glue 2 introduced a set of basic functions on the base Controller object that allow developers to cache values for a configured period of time. These functions are:

AddToCache - This adds a named value to the cache for a period of time.

ExistsInCache - This indicates if a named element exists in the cache.

GetFromCache - This gets the named item from the cache.

RemoveFromCache - This removes a named item from the cache.

See the API documentation for more details on these functions.

Model-Glue 3 builds significantly on top of the caching features of Model-Glue 2. Model-Glue 3 has a granular way to cache the outcome of event handlers and views. However, Model-Glue 3 intentionally does not have an object caching system. This is really a feature of your model and is not one of Model-Glue's concerns.

Caching Configuration

Model-Glue 3's caching systems will, by default, cache elements for five seconds. This default value can be changed in your modelglue.modelGlueConfiguration bean by simply adding or updating a property called defaultCacheTimeout, like this:

<!-- Set the default cache timeout in seconds -->
<property name="defaultCacheTimeout"><value>60</value></property>

This will set the default cache timeout to 60 seconds. Obviously, you can provide whatever default value you want. This value can be overridden within the framework itself.

Using Event-Handler and Include Tag Caching

Developers can use the following arguments on their <event-handler> and <include> view tags to provide caching:

cache (true or false)

This argument indicates if the event handler or view should be cached. If this is provided with no additional cache-related arguments, the entire event-handler or include results will be cached for the default time period.

cacheKeyValues - (a list of values from the event)

This argument can be used to control how an event-handler or included view is cached. The values specified in this argument will be looked up in the event and used to create the key within the cache.

So, for example, let's say you are creating an event-handler named "view.product" that displays a product from your online catalogue. You could set the cacheKeyValues to "productId" to cache the results of the event handler for each unique productId value.

To clarify, when the event handler is rendered for a productId of 3, the caching system will set its key to "view.product.3". (Actually, because of the Formats feature, this would really be "html.view.product.3", assuming you're viewing in HTML format.)

Another example of this feature could be caching the output for a user. So, if you have a sessionId value in the event which was unique for the user, you could set cacheKeyValues to "sessionId" and the system would cache the results of that event handler or included view specificially for that one user.

Finally, because this is a list, you can combine a set of values from the event. You could, for example, cache the view.product event for each individual product an user by setting the cacheKeyValues argument to "productId,sessionId". This might be useful in the case that you display your products differently for each user.

cacheTimeout - (a numeric value)

The cacheTimeout argument sets how long the cached event-handler or included view remains in the cache. By default this is 5 seconds. You can override this value using the defaultCacheTimeout as documented earlier in this document. Or, you can use the cacheTimeout argument on an event-handler or include tag to control how long this item is cached.

The Beans Scope and the CacheAdapter

Model-Glue uses a component known as a CacheAdapter to cache data. The CacheAdapter is placed in the beans scope for all Controllers and is easily accessible there. As an example, to dump this object you could ad this code within a controller:

<cfdump var="#beans.CacheAdapter#" />

This object provides functions you can use to interact with the CacheAdapter. You can, for example, purge the cache, add values, etc. In fact, the legacy AddToCache, ExistsInCache, GetFromCache, and RemoveFromCache functions on the controller simply access the beans-scoped CacheAdapter.

For complete details on the functions on the CacheAdapter see the API documentation.

Creating Custom Caching Systems

Model-Glue's built in caching system is intentionally simple. It simply puts items into a component in the application scope. If you need something more complex you can implement your own caching system.

By creating your own CacheAdapter you could, for example, make use of Memcached or JDBM for an enterprise class caching system.

To do this, you will need to create your own CacheAdapter by either extending the ModelGlue.gesture.externaladapters.contentcaching.SimpleTimedCache or simply implement the ModelGlue.gesture.externaladapters.contentcaching.!IContentCache interface.

Once you've created your cache adapter you simply need to write it in. To do so you will need to add a ColdSpring bean in your ColdSpring configuration file with an ID of "modelglue.cacheAdapter". Here's a basic example:

<bean id="modelGlue.cacheAdapter" class="Model.cache.ExampleCacheAdapter">
  <!-- my configuration properties would go here -->
</bean>

When you restart your Model-Glue application it will start using your new, custom, CacheAdapter.