Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to Generate Unique Cache Keys?

Guide ,
Dec 21, 2012 Dec 21, 2012

I'm trying to improve the performance of my ColdFusion 10 application by making better use of caching.  I have a particular DAO/Gateway CFC that queries a particular database.  I added query caching to the query in this CFC and it made a huge difference.

Since this database is outside my control for edits/updates, I need to be able to initiate flushing of the cached queries for this database only.  I don't believe the built-in ColdFusion ehcache query cache allows for granular control of flushing - in CF Administrator you pretty much can only flush the entire cache.

So I figured the only solution was to create my own cache region that would be associated with just this database, and manually cache the queries into that region.  Then I can flush just that region without impacting any other cached queries.

So here's my difficulty:  I believe that Internally, ColdFusion caches queries using a hash of the generated SQL.  I'm thinking I'll store my queries in cache with a key that hashes the input parameters.  Would the built-in Hash() function be the appropriate mechanism to create cache keys that are unique for each parameter combination?  Does it matter what algorithm I use with Hash()?

Thanks,

-Carl V.

3.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Dec 21, 2012 Dec 21, 2012

I guess I'll answer my own question.  I've tested using Hash() using the MD5 algorithm, and it appears that it generates repeatable unique values for each combination of parameters.  I did this:

  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() with MD5.

This seems to be what I was hoping for.  I would still love to get some confirmation that this is a good approach, or if

...
Translate
Guide ,
Dec 21, 2012 Dec 21, 2012

I guess I'll answer my own question.  I've tested using Hash() using the MD5 algorithm, and it appears that it generates repeatable unique values for each combination of parameters.  I did this:

  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() with MD5.

This seems to be what I was hoping for.  I would still love to get some confirmation that this is a good approach, or if there are better ways to do this.

-Carl V.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 21, 2012 Dec 21, 2012

Sorry, didn't see your original question! You might be better asking this on StackOverflow, as there's more people who have more experience with CF keeping an eye on the questions there.

What I can offer is that MD5 hashes are not guaranteed unique for different input strings. It's unlikely to happen, but it can happen. I think - but am not sure - more "thorough" hashing algorithms might be more likely to have a higher degree of uniqueness.

That said, the general approach is how these things are generally done.  And it's very very unlikely that you'll ever come to use a combination of elements contributing to the key that will generate a hash that you've already used for something else.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Dec 21, 2012 Dec 21, 2012

@Adam,


Thank you for the confirmation.  Maybe I'll switch the algorithm over to SHA, as SHA-256 through SHA-512 seem like overkill.

This is new territory for me, so it's good to get validation on my thought process.  I might just put a question up on StackOverflow later.


Thanks again!

-Carl V.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2012 Dec 22, 2012

Carl Von Stetten wrote:


Would the built-in Hash() function be the appropriate mechanism to create cache keys that are unique for each parameter combination? 

Yes, ColdFusion's hash() is an appropriate mechanism to create such keys. The least troublesome software solutions are those that use built-in resources.

  Does it matter what algorithm I use with Hash()?

No, I shouldn't think so. Even the weakest algorithm available to ColdFusion, like any other hash algorithm, makes use of the avalanche effect. A slight change in the input will result in an avalanche of changes in the output.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Dec 26, 2012 Dec 26, 2012
LATEST

Per @Adam's suggestion, I posted a similar question on StackOverflow.  Someone over there reminded me that ColdFusion 10 now has a "cacheRegion" attribute on the CFQuery tag.  This provides exactly the separation of cached queries that I needed, without having to roll my own unique ID.  If I wanted to still generate my own ID, there is also the "cacheID" attribute on CFQuery that I could use.

-Carl V.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources