Redis: Is there a way to retrieve an array of all cached items that have a key that start with a specific string?
Maybe I have the wrong idea, but let's say I have these keys in redis:
laravel:shopping_cart:1
laravel:shopping_cart:2
laravel:shopping_cart:3
laravel:shopping_cart:4
laravel:shopping_cart:5
Is there a simple way to iterate over any keys that start with "laravel:shopping_cart:" and return an array? Something like:
$shopping_carts = [
1 => // laravel:shopping_cart:1 value,
2 => // laravel:shopping_cart:2 value,
3 => // laravel:shopping_cart:3 value,
4 => // laravel:shopping_cart:4 value,
5 => // laravel:shopping_cart:5 value,
]
I guess I could cheat and do a while loop starting from 0 and test every combination up to a point but that seems sloppy. I can't seem to find any info on retrieving existing values without first knowing the full key. Maybe I'm just missing it or perhaps is this where I get off the Redis train and head over to MYSQL?
Thanks for any insight!
Most redis packages implement the keys() function, or they have a way to execute raw commands without using the cli. I was just pointing you to the generic function that you'd be looking for. Which redis package are you using?
Like with phpredis you can:
$redis->keys('laravel:shopping_cart:*');
Please or to participate in this conversation.