How to get list of all cached items by key in Laravel 5?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Exploring Laravel Cache: Managing and Retrieving Stored Items Efficiently Body: Laravel's Cache class offers various methods to handle cache items, including retrieval, saving, checking existence, etc. However, it might seem challenging to obtain a list of all cached items with specific keys in Laravel 5 using the file-based cache driver without third-party packages or extensive looping. This blog post aims to clarify how you can achieve this in an efficient way. Let's first understand the basic Cache methods: 1. get('itemKey'): Retrieves a specific item from the cache based on its key. 2. remember('itemKey', $expiration, callback): Stores a value in the cache using the specified key. If an expiration date is set, the item will be deleted after that time. 3. forget('itemKey'): Removes a cached item using a given key. 4. has('myKey'): Checks if the cache contains an item with the specified key. Now, let's explore how to obtain a list of all items in the cache. There are several ways: 1. Using artisan commands: Laravel offers cache-related commands that can help retrieve cached data. You can use 'cache:list' command to show all keys with their values or 'cache:dump' for storing all cached data in a file. To get the desired output format, you might need to parse the generated dump file using PHP scripts. 2. Using third-party packages: Many Laravel packages exist that facilitate managing cache items and provide various utilities. One such package is "Laravel-Redis-Dump", which can be used to list all cached data with their keys in a human-readable format. 3. Looping through the keys: If you don't want to install any additional packages, looping through Cache::has() method for each key could work. However, this is not the most efficient approach and may result in a significant performance impact if there are numerous keys or cached items. A better way would be to use generators that can iterate over cache keys and values without affecting overall performance much. Here's an example: ```php use Illuminate\Support\Facades\Cache; foreach (Cache::getFacadeRoot()->allKeys() as $key) { if (Cache::has($key)) { // Process the cached item echo "Key: {$key} - Value: ".Cache::get($key)."\n"; } else { continue; } } ``` 4. Using custom functions: Laravel's Service Provider system enables you to create custom functions for specific tasks, including dumping cached data. A function like this could be useful: ```php allKeys() as $key) { if ($value = Cache::get($key)) { // Format the value for better readability $formattedValue = preg_replace('/[^a-zA-Z0-9\_]/', '', $value); $json[] = [ 'Key' => $key, 'Value' => $formattedValue ]; } else { continue; } } return json_encode($json, JSON_PRETTY_PRINT); } ``` Conclusion: While Laravel's Cache methods provide various ways to handle cache items, obtaining a list of all cached items directly without additional packages or extensive looping might not be the most straightforward approach. However, with the given techniques, you can achieve a workaround solution that doesn't negatively impact performance. It is always recommended to use existing tools and best practices, and only resort to custom implementations when required. Remember to keep your code maintainable and clean for better development experience in the long run.