Little question about Cache's reset() function

46 views
Skip to first unread message

Vahrokh Vain

unread,
Dec 18, 2016, 12:18:51 PM12/18/16
to Fat-Free Framework
Hello,

I am using since a while the new and awesome "cache tags" feature that has been kindly added to F3.

Recently I needed to add some new functionality to my application and this functionality requires for whole groups of cached tags to be deleted from memcache.


That is, on memcache I have this kind of cache keys:

k7y6k0qmlx0f.2hfzyz5bm6uck.comp_company_rows_1_37d26pw12t8g8_25_1.sql
k7y6k0qmlx0f
.2od9tulaeeqs0.comp_company_rows_1_37d26pw12t8g8_25_2.sql
k7y6k0qmlx0f
.2oq00v8k78cg0.comp_company_detail_reg_1.sql
k7y6k0qmlx0f
.3cbn22h9crggk.comp_company_rows_1_37d26pw12t8g8_25_0.sql
k7y6k0qmlx0f
.6lk9rcohc7w8.comp_company_rows_1_37d26pw12t8g8_25_3.sql

In particular, I have keys created from a base cache tag name: "comp_company_rows_", formatted with a number of additional numbers like 1, hash(string), 25, 1...N and similar, used to make them unique. The resulting "complex", formatted full tag names were then used to store query results on memcache.

Now, I need to delete every keys starting with
comp_company_rows_

What took me by surprise is finding out how if I call this:

Cache::instance()->reset('comp_company_rows_');

it does exactly what I need! That is, it deletes every memcache key whose tag starts with that prefix and not just the exact matches!


My question is: since it looks too good to be true, is this the officially intended and supported behavior for reset()?

Or is this unintended and I can expect this perfect solution (for me) to be removed in the future?


Thanks in advance!

ved

unread,
Dec 18, 2016, 1:05:41 PM12/18/16
to f3-fra...@googlegroups.com
It should be the intended behavior.

The regex reset() uses is:

$regex='/'.preg_quote($this->prefix.'.','/').'.+?'.preg_quote($suffix,'/').'/';

In your case, this will create a string like:

/k7y6k0qmlx0f\..+?comp_company_rows_/

It then loops over memcache keys and deletes all that match it.

You can see a test here: https://regex101.com/r/vyjHgt/1



 

Vahrokh Vain

unread,
Dec 18, 2016, 4:38:17 PM12/18/16
to Fat-Free Framework
Thank you very much, you went so far to test the regex with my tags!

ved

unread,
Dec 18, 2016, 5:15:57 PM12/18/16
to Fat-Free Framework
No problem, I actually didn't know it worked this way as I never needed that functionality.


You can use $suffix to only clear cache entries with a suffix that matches this string.

which can be ambiguous since a partial match seems to work.

I'm still pretty sure it's the intended behavior as your example clearly shows it's the logical thing to do.
Reply all
Reply to author
Forward
0 new messages