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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing Cache Metadata in Laravel 5 File Drivers

The standard Cache facade in Laravel provides robust methods for retrieving and storing data, such as get() and remember(). There is also a convenient has() method to verify existence. However, a common architectural need arises when you require visibility into the entire cache store without knowing specific keys beforehand. You might be looking for a utility function similar to Cache::all() that returns an associative array of every stored item.

In Laravel 5, specifically when utilizing the file-based cache driver, there is no built-in facade method that directly exposes this list of cached items. The abstraction layer does not provide a way to iterate over the internal store contents programmatically from the application code. This limitation exists because cache drivers are designed around key-value retrieval patterns, not bulk enumeration.

To achieve your goal of listing all cached items in a file-based driver, you must bypass the Cache facade and interact directly with the underlying storage mechanism. The file-based driver stores data as serialized PHP arrays within files inside the configured cache directory (typically storage/framework/cache/data).

You can implement a custom solution by accessing this directory and parsing the files manually. Here is a practical approach using PHP's filesystem functions:

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

function listCachedItems()
{
    // Determine the path to the cache directory based on the current application configuration
    $cachePath = Cache::getStorePath();

    if (!is_dir($cachePath)) {
        return [];
    }

    $items = [];

    foreach (glob($cachePath . '/*') as $file) {
        $filename = basename($file);

        // Skip lock files or other metadata files that are not cache items
        if ($filename === '.lock' || Str::startsWith($filename, '_')) {
            continue;
        }

        try {
            $content = file_get_contents($file);
            
            // Deserialize the content. Note: This assumes standard PHP serialization format used by Laravel 5
            $data = unserialize($content);

            // The filename usually contains the key name, though formats vary slightly between versions.
            // In Laravel 5 file driver, the key is often derived from the filename.
            $key = explode('.', $filename)[0]; 

            $items[$key] = $data;
        } catch (Exception $e) {
            // Handle cases where files might be corrupted or inaccessible
            continue;
        }
    }

    return $items;
}

This approach returns a structure similar to your requirement:

[
    'itemKey' => ['myData1', 'myData2'],
    'myKey'   => 'foo'
]

However, you must be aware of significant trade-offs when using this method. The primary concern is performance and security. Parsing every file in the cache directory can be resource-intensive, especially if your application has accumulated many small cached items or large serialized blobs. Furthermore, deserializing arbitrary data from files carries inherent risks; while Laravel validates data before storing it, manually reading files requires you to ensure you are not exposing sensitive logic that might depend on the internal structure of the cache entries.

Another critical consideration is consistency. The file-based driver does not guarantee that a list generated this way represents a consistent state at any given millisecond. If another process modifies the cache while you are iterating, the resulting array might reflect partial updates or race conditions. Additionally, this method relies on the specific internal implementation of the file driver in Laravel 5. Future versions or different drivers (like Redis or Memcached) will not support this filesystem-based enumeration strategy.

If your need for a full list of keys is frequent, consider whether you truly require the values as well. Often, developers only need the keys to perform cleanup operations or debugging. In such cases, generating a list of keys via Cache::get with a wildcard pattern (if supported by your specific driver configuration) or maintaining a separate metadata table in the database might be more scalable solutions than parsing filesystem blobs directly.

For production environments where cache introspection is critical, it is advisable to avoid relying on internal file structures unless absolutely necessary. Instead, design your caching strategy so that keys are managed explicitly, perhaps through a dedicated command or event listener that tracks active keys in a separate, queryable location.

Our network