Cache Stores

Cache stores are a fundamental component of Guardian, playing a crucial role in its rate limiting and error handling functionality. These stores provide the underlying mechanism for persisting and retrieving data related to request limits, error counts, and other essential metrics. By offering various cache store implementations, Guardian ensures flexibility and adaptability across different environments and requirements, allowing for efficient management of rate limiting rules and error handling policies.

Available Cache Stores

Guardian provides multiple cache store implementations to suit different environments and requirements. This document outlines the available cache stores and how to construct them.

LaravelStore

The LaravelStore is designed for use within Laravel applications. It utilizes Laravel's built-in caching system.

Construction

use Illuminate\Cache\Repository;
use Midnite81\Guardian\Store\LaravelStore;

$laravelCache = app(Repository::class);
$store = new LaravelStore($laravelCache);

FileStore

The FileStore uses the local filesystem for caching. It's suitable for applications without a dedicated caching system.

Construction

use Midnite81\Guardian\Store\FileStore;

$basePath = '/path/to/cache/directory';
$store = new FileStore($basePath);

Important Notes

  1. Permissions: Ensure that your application has the necessary read and write permissions for the specified cache directory. The FileStore needs to be able to create, read, update, and delete files in this directory.

  2. Directory Creation: The FileStore will attempt to create the cache directory if it doesn't exist. However, it needs the appropriate permissions to do so.

  3. Security: Choose a directory that is not publicly accessible to prevent unauthorized access to cached data.

  4. Disk Space: Monitor the disk space usage of your cache directory, especially for long-running applications or those with high cache volume.

Example of setting proper permissions (Unix-based systems):

sudo mkdir -p /path/to/cache/directory
sudo chown -R www-data:www-data /path/to/cache/directory
sudo chmod -R 755 /path/to/cache/directory

Replace www-data with the user under which your web server runs.

DatabaseStore

The DatabaseStore uses a database table for caching. It's useful when you want to persist cache data in a database.

Important: The DatabaseStore will automatically create the necessary caching table if it doesn't exist.

Construction

use PDO;
use Midnite81\Guardian\Store\DatabaseStore;

$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
$tableName = 'guardian_cache'; // optional, defaults to 'guardian_cache'
$store = new DatabaseStore($pdo, $tableName);

When you create a new instance of DatabaseStore, it will:

  1. Check if the specified table (default: 'guardian_cache') exists.

  2. If the table doesn't exist, it will automatically create it with the following structure:

    • key (VARCHAR(255), PRIMARY KEY)

    • value (TEXT)

    • expiration (INT)

This means you don't need to manually create the table or run any migrations. The DatabaseStore handles the table creation for you.

RedisStore

The RedisStore uses Redis for caching, providing fast in-memory caching with persistence.

Construction

use Redis;
use Midnite81\Guardian\Store\RedisStore;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$prefix = 'guardian:'; // optional, defaults to 'guardian:'
$store = new RedisStore($redis, $prefix);

Using Cache Stores with Guardian

Once you've constructed a cache store, you can use it when creating a Guardian instance:

use Midnite81\Guardian\Guardian;

$guardian = new Guardian('your-identifier', $store);

Replace $store with your chosen cache store instance.

Choosing a Cache Store

  • LaravelStore: Best for Laravel applications, as it integrates seamlessly with Laravel's caching system.

  • FileStore: Good for simple applications or development environments where you don't want to set up a separate caching system. Ensure proper file permissions are set.

  • DatabaseStore: Useful when you want to persist cache data in a database, which can be beneficial for debugging or data analysis. It automatically creates the required table.

  • RedisStore: Excellent for high-performance requirements, as Redis provides fast in-memory caching with optional persistence.

Choose the cache store that best fits your application's needs and infrastructure.

Last updated