Response Caching for RESTfulCF

I've just added response caching to RESTfulCF. For successful, cacheable controller actions (GET requests only), the pre-rendered response is cached, so on the next request the controller action can be completely skipped.

An example application-scope cache is provided, but you shouldn't really use this for production. But as an example of how simple it is to enable response caching, you'd first need to add the cache instance in your Dispatcher:

<cfset setCache(createobject("component", "restfulcf.framework.core.cache.ApplicationCache").init())>

Then, in the relevant Controller function, set that the response can be cached as follows:

<cfset arguments['_response'].setCacheStatus(TRUE)>

That's it, you're done! All successful (200 status code) GET requests that get routed through this controller method will now be cached.

At White Label Dating we're using a memcached-based cache for storing reporting-related requests that can take a few seconds to pull together; see the RESTfulCF README file for details on creating your own cache.

Dear RESTafarians: Introducing RESTfulCF

Dear RESTafarians,

I thought I’d take the opportunity to introduce this new CF/REST project: RESTfulCF; it’s a framework for publishing a REST interface to a ColdFusion application. The code is freely available. Bug reports should be made on the RESTfulCF issue tracker.

Everywhere you look now, people are opening up their data via APIs: Google, Flickr, Twitter, Facebook - the list goes on and on. You can find an API for anything from discovering who your local MP is, finding song lyrics, or discovering new species of fungi and micro-organisms. RESTfulCF provides a framework for publishing a REST API of your own.

The framework has been extracted from some of my recent work on the White Label Dating platform. We needed to be able to provide a simple REST interface on top of our existing ColdFusion app for use on various key internal projects, including a new Ruby on Rails system, and a Javascript-based reporting dashboard. The framework is currently in production use on the White Label Dating platform.

The basic principles of RESTfulCF are:

  • Resources are implemented as components, with properties of the resource defined using <cfproperty> tags. onMissingMethod() is used to provide implicit setter/getter functions for those properties;
  • There are default representations which apply to every resources (unless overridden within the resource component itself), which means writing a lot less code;
  • Controllers are the “glue” that makes the system work: functions are defined to handle each type of request (read all resources, read one resource, create a new resource etc) that may be made of a resource;
  • Convention-over-configuration for defining routes: a single function call of addResource("world") will automatically map common URI patterns to a Worlds controller, for example `GET /worlds/:id` will be mapped to the `read` function.

RESTfulCF also provides a number of other helpful features:

  • automatic resource collection handling;
  • standardised error reporting and attribute typing (including precision) in XML representations, allowing simple consumption of the API by Ruby in Rails using ActiveResource (or HyperActiveResource);
  • very simple to work with: an endpoint file, a dispatcher, plus one or more controllers and resource description files, and you’re up and running.

In general, RESTfulCF has been designed to provide a lightweight interface sitting on top of an existing business logic layer, rather than to provide the whole application itself.

Please use and abuse the framework: check out the sample application, write your own resources and controllers and let me know your thoughts. Or, if you feel adventurous, fork it on GitHub and check something off the to-do list.

Let your data loose!

BlogCFC was created by Raymond Camden. This blog is running version 5.5.006. | Protected by Akismet | Blog with WordPress