I have a render array I'm caching as follows:
$build['friend_link'] = [ '#type' => 'item', '#markup' => $link, '#cache' => [ 'keys' => ['friendship', $user->id()], ], ];
I need to delete this cache when the friendship is invalidated. However, I'm having a lot of troubles with retrieving the cache in order to invalidate it. I'm attempting to use the following code to retrieve the cache item:
$render_cache = \Drupal::cache('render'); $render_cache->get('friendship:' . $friend->id()); // id = 4
However, this doesn't work, because the actual cache key in the DB is:
I tried setting the cache contexts to an empty array:
'#cache' => [ 'keys' => ['friendship', $user->id()], 'contexts' => , ],
But this didn't change the key. So I set the contexts to a value:
'#cache' => [ 'keys' => ['friendship', $user->id()], 'contexts' => ['user'], ],
This changed the key to:
So while this does make it potentially easier to build the key manually, it seems like I'm missing something.
I did find RenderCache::createCacheID(), however it's protected so I can't call it.
I'm at a bit of a loss here, is there an API function I'm missing somewhere to retrieve the cache?