Showing posts with label memcached. Show all posts
Showing posts with label memcached. Show all posts

Monday, January 12, 2009

A few things about onMissingMethod()

ColdFusion 8 includes the onMissingMethod event handler, a very simple yet powerful feature that can be used in a variety of ways, from implicit getters and setters to method interceptors and more.

I've stumbled upon 2 things worth mentioning that didn't seem apparent at first.

1. onMissingMethod only works for public method calls. If you try to access a function remotely, such as http://mysite.com/components/foo.cfc?method=bar, and the bar() method doesn't exist, onMissingMethod won't catch it.

2. If you're using memcached to cache your objects, onMissingMethod won't fire at all. This seems to make sense, since ColdFusion appears to fire onMissingMethod at runtime.

Sunday, January 11, 2009

AOP, Caching, and memcached

Last week I started playing around with Aspect-Oriented Programming (AOP) in ColdSpring and was pretty amazed at how useful it could be. To be able to alter the functionality of your application without touching your core application code is an extremely powerful concept, but I wasn't sure where to begin.

The example on the ColdSpring site shows how AOP can be applied to handle logging across an application, but that doesn't seem all that useful (or sexy) to me. I wanted a better scenario.

After reviewing some code in a project I've been working on, I noticed a prime example of where I could use AOP: caching.

There was code inside multiple function calls that would perform caching based on the component, method, and arguments being passed into the method. The code was using non-deterministic caching, meaning if the data didn't exist in the cache, the system would fetch the data then put it into the cache. The caching code looks similar to this:


<cfset key = application.cache.getKey("reportDAO","getReportByID",arguments) />

<cfif application.cache.hasData(key)>

<cfset report = application.cache.getData(key) />

<cfelse>

... build the report ...

<cfset application.cache.setData(key,report) />

</cfif>


The code worked fine, but I had to add those same 4 lines of code to each method I wanted to cache. Plus, it led to a low level of cohesion; the method needed to know how to build a report as well as how to store and retrieve itself in memory.

With AOP, I could now configure ColdSpring to intercept each method call I wanted to cache without having to flood my code with caching logic.

As a nice side bonus, the non-deterministic key/value caching strategy I was using turned out to be similar to how memcached, a high-performance, distributed memory object caching system, caches its data. Knowing this, I was able to modify the caching advice component I created and plugin memcached in a matter of minutes.

I'm currently working on an XML based caching component that will handle bean/method caching in the request scope, session scope, and memcached. I'll post more details when I have something to show.