Search code examples
coldfusioncoldfusion-10

How to generate unqiue keys for caching items in ColdFusion


I posted a similar question over on the Adobe Community forums, but it was suggested to ask over here as well.

I'm trying to cache distinct queries associated with a particular database, and need to be able to flush all of the queries for that database while leaving other cached queries intact. So I figured I'd take advantage of ColdFusion's ehcache capabilities. I created a specific cache region to use for queries from this particular database, so I can use cacheRemoveAll(myRegionName) to flush those stored queries.

Since I need each distinct query to be cached and retrievable easily, I figured I'd hash the query parameters into a unique string that I would use for the cache key for each query. Here's the approach I've tried so far:

  1. Create a Struct containing key value pairs of the parameters (parameter name, parameter value).
  2. Convert the Struct to a String using SerializeJSON().
  3. Hash the String using Hash().

Does this approach make sense? I'm wondering how others have approached cache key generation. Also, is the "MD5" algorithm adequate for this purpose, and will it guarantee unique key generation, or do I need to use "SHA"?


Solution

  • UPDATE: use cacheRegion attribute introduced in CF10!

    http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fae.html

    Then all you need to do is to specify cachedAfter or cachedWithin, and forget about how to to generate unique keys. CF will do it for you by 'hashing':

    • query "Name"
    • SQL statement
    • Datasource
    • Username and password
    • DBTYPE

    reference: http://www.coldfusionmuse.com/index.cfm/2010/9/19/safe.caching

    I think this would be the easiest, unless you really need to fetch a specific query by a key, then u can feed your own hash using cacheID, another new attribute introduced in CF10.