<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: James Moberg</title>
    <description>The latest articles on DEV Community by James Moberg (@jamoca).</description>
    <link>https://dev.to/jamoca</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F813840%2F15ac35ef-bec1-48d5-a597-eb79b9715364.png</url>
      <title>DEV Community: James Moberg</title>
      <link>https://dev.to/jamoca</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jamoca"/>
    <language>en</language>
    <item>
      <title>Caching Repetitive UDF Access to Request Scope Using Argument Stringified+Hashed Key</title>
      <dc:creator>James Moberg</dc:creator>
      <pubDate>Thu, 08 Dec 2022 17:38:23 +0000</pubDate>
      <link>https://dev.to/jamoca/caching-repetitive-udf-access-to-request-scope-using-argument-stringifiedhashed-key-3lln</link>
      <guid>https://dev.to/jamoca/caching-repetitive-udf-access-to-request-scope-using-argument-stringifiedhashed-key-3lln</guid>
      <description>&lt;p&gt;While viewing &lt;a href="https://www.fusion-reactor.com/" rel="noopener noreferrer"&gt;FusionReactor&lt;/a&gt; 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 &lt;strong&gt;42ms&lt;/strong&gt; per execution.  I checked the UDF that was performing the query (using &lt;a href="https://github.com/coldbox-modules/qb" rel="noopener noreferrer"&gt;QB&lt;/a&gt;) and the query was configured to be cached for 5 minutes... but overhead-wise, it was still taking &lt;strong&gt;42ms&lt;/strong&gt; 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 &lt;strong&gt;1,452ms&lt;/strong&gt; 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 &lt;code&gt;cachePut&lt;/code&gt;/&lt;code&gt;cacheGet&lt;/code&gt; since the caching only needs to live for a single request of "repetitive access".&lt;/p&gt;

&lt;p&gt;I've been playing with the java &lt;code&gt;hashCode()&lt;/code&gt; 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).&lt;/p&gt;

&lt;p&gt;Here's a sample UDF with some slow/fake business logic (emulated using &lt;code&gt;sleep(1000)&lt;/code&gt;). The first request is normally slow (as expected) whereas all repeat accesses with the same exact arguments are &lt;strong&gt;0ms&lt;/strong&gt;. 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 &lt;code&gt;CFLock&lt;/code&gt; with request scope locking?)&lt;/p&gt;

&lt;h2&gt;
  
  
  Source Code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/JamoCA/62f8535cfcf5dacd4e7f5ea678038330" rel="noopener noreferrer"&gt;https://gist.github.com/JamoCA/62f8535cfcf5dacd4e7f5ea678038330&lt;/a&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
      <category>discuss</category>
      <category>career</category>
    </item>
  </channel>
</rss>
