Delete cache entry by key starting with

Hey,

I’m using \Yii::$app->userCache; to store calculated results in memcache.

The keys of the cache entry are composed of the following parts:
1.) a prefix, that stands for the class of feature or action, like counts, userdata
and
2.) a value-suffix for the keys, like comments, posts, …

So I have cache keys, like counts_comments or counts_posts.
Some of the value-suffixes are very dynamically, like an hash of serialized key-array of all often needed posts, where the cache-key looks like this: counts_4b3da834d3e1a

In some situations, I need to delete all cache entries starting with the same prefix.
I need something like this:
$cache = \Yii::$app->userCache; $cache->delete('counts_*');

Looking at the documentation:

I found the following information about the $key in delete($key);:

This can be a simple string or a complex data structure consisting of factors representing the key.

What they mean with complex data structure and can I fulfill my requirement with it?
Unfortunately I am missing a code example here.

To answer my own question in the case somebody has the same question.

I did not find any solution how to remove all cache entries with an key “starting with counts_”. But I did use caching tag dependency (TagDependency, yii\caching\TagDependency | API Documentation for Yii 2.0 | Yii PHP Framework)

This mean: I add an Cache tag while setting the cache entry.


use yii\caching\TagDependency;


$cacheDepCounts = new TagDependency(['tags' => 'counts']);
$cacheDepPosts = new TagDependency(['tags' => 'posts']);

$cache->set('counts_4b3da834d3e1a', <valueX>, null,  $cacheDepCounts);
$cache->set('counts_27d921af85ebc3', <valueY>, null,  $cacheDepCounts);
$cache->set('posts_foo', <valueA>, null,  $cacheDepPosts);

To remove all the cache entries containing counts-stuff I use:

TagDependency::invalidate($cache, 'counts');