Another caching question

So my last question was about the ‘dependson’ cache feature in ACF, this
feature is not in lucee and I was directed to create my own kind of cache
using setcache, getcache etc. Is the idea here to create a unique cacheID
for each cached item and then when updating specific items on my site, to
expire that cache and set a new ID and then to get the cache I would have
to track each cacheID?

I’m still trying to figure out the best way of doing this because as noted
before I use the depends on tag to determine if the cache should be updated
( using a cache key I generate from the DB ) If the cacheKey is differnt
then I expire the cache, otherwise it will not expire. I need to have the
ability to expire some cache but not all.

Any help would be appreciated.

Thanks.

Not knowing your application structure, your database structure, or what
you’re trying to accomplish with either… I’ll take a stab at helping you
as best I can anyway :wink:

dependsOn doesn’t generally expire any cached data I don’t believe, it
simply checks the cache to see if a cached object exists that matches the
dependsOn values. Given that, and given the little bit of information
you’re provided about your environment, I’d say something along the lines
of the following would get you going in the right direction…

// get your cache id from the database based on the page id qGetData = DAO.getData( pageId );

// get the cached data from the cache
pageFragment = cacheGet( ‘pageFragment_’ & hash( qGetData.cacheId &
serializeJson( URL ) ) );

// check if it is null
if( isNull( pageFragment ) ) {
// it is null (so the cached data doesn’t exist), generate the page
fragment with a function
pageFragment = generatePageFragment();
// and save the page fragment to the cache (for the next request)
cachePut( ‘pageFragment_’ & hash( qGetData.cacheId & serializeJson( URL )
), pageFragment, createTimeSpan( 30, 0, 0, 0 ), createTimeSpan(30, 0, 0, 0
) );
}

// output the page fragment
writeOutput( pageFragment );

In short, you try to get the cached object using a combination of your
cache key (cacheId) and whatever scope you’re trying to capture dependsOn
values from now (assuming the URL scope, but could be FORM or, if using a
framework, rc scope) and if it returns null then it doesn’t exist, so you
generate the output, save it to the cache (for the next request) and output
the generated content. If it isn’t null, then you have the cached version
already and just output that.

HTH

– Denny

Sorry, copy & paste dumbness on my last reply… using specific keys from
the URL would not use serializeJSON, it should just be:

cacheGet/cachePut( ‘getFreeForm_’ & hash( updated.updateID & URL.dependantKey
& URL.dependantKey2 […] ) );

Sorry about any confusion :wink:

– Denny

Hi Denny,

So what it sounds like is essentially my ‘cacheKey’ would be used as the
depends on but would be hashed along with say the page name, so where you
have pageFragment = cacheGet I would set say the page name so I might have
something like

‘getFreeForm_’ & hash( cacheKey)

This would check if there is a cache, if not creates one otherwise get that
cached item

Correct?

I really should have said this to begin with but the way I use the cache
in ACF is like this

Per the ACF docs the useQuerString and the dependsOn tags used togeather
will generate a new template cache based on the querystring and the depends
on

per the docs for querystring.

"If true, generates a template cache ID that includes the query string.
This means that a new template cache is created whenever the query string
changes.

If set to true, the attribute dependson considers the URL parameters
defined in the query string as well to generate template caches.

Also see the Usage section"

And for the depends on

A comma separated list of variables. If any of the variable values change,
ColdFusion updates the cache. This attribute can take an expression that
returns a list of variables.

The code you mentioned above will cache the page based on a hash of the
cacheID, would expanding your code to grab the querystring AND cachekey
essentially do the same thing as I’m doing now?

Thanks!On Tuesday, October 18, 2016 at 7:56:03 AM UTC-7, Denard Springle wrote:

Not knowing your application structure, your database structure, or what
you’re trying to accomplish with either… I’ll take a stab at helping you
as best I can anyway :wink:

dependsOn doesn’t generally expire any cached data I don’t believe, it
simply checks the cache to see if a cached object exists that matches the
dependsOn values. Given that, and given the little bit of information
you’re provided about your environment, I’d say something along the lines
of the following would get you going in the right direction…

// get your cache id from the database based on the page id qGetData = DAO.getData( pageId );

// get the cached data from the cache
pageFragment = cacheGet( ‘pageFragment_’ & hash( qGetData.cacheId &
serializeJson( URL ) ) );

// check if it is null
if( isNull( pageFragment ) ) {
// it is null (so the cached data doesn’t exist), generate the page
fragment with a function
pageFragment = generatePageFragment();
// and save the page fragment to the cache (for the next request)
cachePut( ‘pageFragment_’ & hash( qGetData.cacheId & serializeJson( URL )
), pageFragment, createTimeSpan( 30, 0, 0, 0 ), createTimeSpan(30, 0, 0, 0
) );
}

// output the page fragment
writeOutput( pageFragment );

In short, you try to get the cached object using a combination of your
cache key (cacheId) and whatever scope you’re trying to capture dependsOn
values from now (assuming the URL scope, but could be FORM or, if using a
framework, rc scope) and if it returns null then it doesn’t exist, so you
generate the output, save it to the cache (for the next request) and output
the generated content. If it isn’t null, then you have the cached version
already and just output that.

HTH

– Denny

Hey MK,

I’m a bit confused by your code - what does updated.updateID in your
dependsOn represent? If this is an ID from the database, does your
queryString have that value as a key in the query? Example: Let’s assume
updated.updateID equals 12, does your query string have 12= in
it? If not, then dependsOn isn’t doing you any good, currently, I don’t
think. dependsOn should refer to the key (parameter) in your queryString
that the cache should use to determine if the value (of that key/parameter)
has changed and the page/fragment/object should be regenerated. It limits
what key(s) within the query string is used to determine if the cache
should be updated, versus using the entire queryString which it would do if
dependsOn isn’t specified. In ACF’s case I’m sure they probably just ignore
any dependsOn keys which are not in the queryString and simply use the
query string to build/manage the cache. Lucee might not be so adept which
might be why you’re running into those issues?

All that said, you could expand on my example and use the page name
instead of ‘pageFragment_’ and then hash a combination of your ID and the
values of whichever query string keys you would want your cache to be
dependent on, something like:

cacheGet/cachePut( ‘getFreeForm_’ & hash( updated.updateID & serializeJson(
URL.dependantKey & URL.dependantKey2 […] ) ) );

If you only want the cache to be dependent on the updated.updateID, then
simply do:

cacheGet/cachePut( ‘getFreeForm_’ & hash( updated.updateID ) );

HTH

– Denny

Hi Denny,

So the way things work is I have a query string that contains a accountID,
this account ID is used to query the DB and grab the ‘cacheKey’ from the
DB. I then use this key as the ‘dependsOn’ value so basically I’m using the
queryString AND the dependsOn values to generate the Cache. According to
the ACF docs when used together it uses both dependsOn and QueryString.

This being said I believe if I use the code you have provided I can create
a similar function by grabbing the values and the key and doing a ‘hash’ of
them to generate the cacheID, this should be similar to what ACF does I
think :slight_smile:

Let me give this a try and see if this works.

Thanks Denny!On Wednesday, October 19, 2016 at 11:26:07 AM UTC-7, Denard Springle wrote:

Hey MK,

I’m a bit confused by your code - what does updated.updateID in your
dependsOn represent? If this is an ID from the database, does your
queryString have that value as a key in the query? Example: Let’s assume
updated.updateID equals 12, does your query string have 12= in
it? If not, then dependsOn isn’t doing you any good, currently, I don’t
think. dependsOn should refer to the key (parameter) in your queryString
that the cache should use to determine if the value (of that key/parameter)
has changed and the page/fragment/object should be regenerated. It limits
what key(s) within the query string is used to determine if the cache
should be updated, versus using the entire queryString which it would do if
dependsOn isn’t specified. In ACF’s case I’m sure they probably just ignore
any dependsOn keys which are not in the queryString and simply use the
query string to build/manage the cache. Lucee might not be so adept which
might be why you’re running into those issues?

All that said, you could expand on my example and use the page name
instead of ‘pageFragment_’ and then hash a combination of your ID and the
values of whichever query string keys you would want your cache to be
dependent on, something like:

cacheGet/cachePut( ‘getFreeForm_’ & hash( updated.updateID & serializeJson
( URL.dependantKey & URL.dependantKey2 […] ) ) );

If you only want the cache to be dependent on the updated.updateID,
then simply do:

cacheGet/cachePut( ‘getFreeForm_’ & hash( updated.updateID ) );

HTH

– Denny