DEV Community

Cover image for Caching Repetitive UDF Access to Request Scope Using Argument Stringified+Hashed Key
James Moberg
James Moberg

Posted on

Caching Repetitive UDF Access to Request Scope Using Argument Stringified+Hashed Key

While viewing FusionReactor logs for a ColdFusion app, I noticed lots of repetitive JDBC entries in a request that contained the same exact SQL statement and was taking 42ms per execution. I checked the UDF that was performing the query (using QB) and the query was configured to be cached for 5 minutes... but overhead-wise, it was still taking 42ms each time. The "Number of Queries" for the request was 57 and most of the queries were similar cached 40ms lookups and it started adding up to 1,452ms overall. I wondered if there was anything I could do to add self-contained caching to UDFs that could benefit from them. I didn't want to save the response to the session (YIKES! I've seen some code that does this) or use cachePut/cacheGet since the caching only needs to live for a single request of "repetitive access".

I've been playing with the java hashCode() function to convert strings to a unique signed integer and wondered if I could stringify the arguments passed to create a cacheable key and very temporarily store the result in the request scope. This works (for me) and I'm happy with the results, but I wonder if there are any potential issues or side effects that I'm not considering (besides the rare chance of hashing entropy).

Here's a sample UDF with some slow/fake business logic (emulated using sleep(1000)). The first request is normally slow (as expected) whereas all repeat accesses with the same exact arguments are 0ms. I believe that this approach may also work in environments that use multithreaded processing on a single request, but I'm not 100% sure. (ie, in a multithreaded environment, would it be recommended to use CFLock with request scope locking?)

Source Code

https://gist.github.com/JamoCA/62f8535cfcf5dacd4e7f5ea678038330

<!--- 20221208 Caching repetitive UDF access to request scope using argument stringified+hashed key #ColdFusion #cfml
By James Moberg / SunStar Media
GIST: https://gist.github.com/JamoCA/62f8535cfcf5dacd4e7f5ea678038330
TWEET: https://twitter.com/gamesover/status/1600908166426136576
BLOG: https://dev.to/gamesover/caching-repetitive-udf-access-to-request-scope-using-argument-stringifiedhashed-key-4fd6
--->
<cfscript>
string function slowFunction(required a, b="100", c="abc", d=[], e={}) hint="I perform a repetitive function with a cacheable result" {
// generate a unique key based on stringified+hashed arguments passed to UDF
local.cachekey = "udf_slowFunction_#arguments.toString().hashCode()#";
// if key exists within request scope, return value
if (structkeyexists(request, local.cachekey)){
return request[local.cachekey];
}
// Perform business logic (DB operations, file system, etc)
sleep(1000);
local.result = "#local.cachekey# = #arguments.toString()#";
// copy return response to request scope
request[local.cachekey] = local.result;
return request[local.cachekey];
}
for (a=1; a lte 3; a+=1){
writeoutput("<h3>Iteration #a#</h3><ol>");
for (i=1; i lte 3; i+=1){
t = gettickcount();
result = slowFunction(a);
writeoutput("<li><b>#gettickcount()-t# ms:</b> #result#</li>");
}
writeoutput("</ol>");
}
</cfscript>

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay