Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How to get list of cache items with specific prefix and value?

Hello,

I am inserting the session id to the cache with the $user_id as the value for every user session:

Cache::put('session_test_' . $session_id ,$user_id, $seconds);

If a user logs from multiple browsers for example, he should have multiple cache items with his $user_id as the value.

How can I retrieve all the cache items that have the session_test_ prefix and the $user_id value?

Thanks

0 likes
3 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Thought we had sorted this out 2-3 weeks ago?

You can only retrieve from session if you know the key which is why it was suggested that you put all the users in a single array that you can load and access the correct value.

1 like
Ligonsker's avatar

@Snapey Oh it was something similar but not specifically about that. But you are right. I just made a new one because I noticed Cache has an option to set a cache item for a set amount of time:

Cache::put('key', 'value', $seconds = 10);

Now I will have to do something like having a key which is the user ID, and the value is an array right? In this case I won't be able to set each value in the array for a set amount of time, then I will need to manually do timestamps, and check against them right? (Which is the part we were talking, and now I understand why)

Please or to participate in this conversation.