Cache

A minimalist SQLite based caching system for Laravel applications, with tagging and an option for encryption at rest.

Installation

composer require grafite/cache

Then add the SQLite store to the stores array in your config/cache.php file.

'sqlite' => [
    'driver' => 'sqlite',
    'table' => 'cache',
    'database' => env('CACHE_DATABASE', database_path('cache.sqlite')),
    'prefix' => '',
    'encrypted' => false,
],

To make it your default cache, set the store in your .env file.

CACHE_STORE=sqlite

Create the Database

Generate the SQLite database for caching.

php artisan sql-cache:make:database

This will also create the cache table by default. The database file is created at the path set in cache.stores.sqlite.database, so you can move it anywhere with the CACHE_DATABASE environment variable.

Encryption

Set 'encrypted' => true in the store config to encrypt cached values at rest. Values are encrypted with your application's APP_KEY, so rotating the key will make existing cache entries unreadable.

Tags

The SQLite store supports Laravel's native cache tags.

Cache::tags(['users', 'reports'])->put('report-42', $report, 3600);

Cache::tags(['users', 'reports'])->get('report-42');

Cache::tags(['reports'])->flush();

Tagged Keys

You can also build the tags into the key itself with the key() macro. This gives you a regular cache key, which you can then look up or clear by tag.

$key = Cache::key('report-42', ['users', 'reports']); // report-42:users|reports

Cache::put($key, $report, 3600);
Cache::get($key);

The key cannot contain a :, and the combined key and tags cannot be longer than 255 characters. Either case throws an exception.

Cache::getTag('users'); // collection of the values for every key tagged with users
Cache::forgetTag('users'); // remove every key tagged with users
Cache::forgetTags(['users', 'reports']); // remove keys tagged with all of the given tags

Forget Like

Remove every cache entry whose key contains the given string.

Cache::forgetLike('report-');

Create the Cache Locks Table

php artisan sql-cache:make:locks-table

Purge the Cache Locks

Clear all existing cache locks to prevent stale locks from affecting your application.

php artisan sql-cache:purge-locks