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
FileStore
The FileStore
uses the local filesystem for caching. It's suitable for applications without a dedicated caching system.
Construction
Important Notes
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.
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.
Security: Choose a directory that is not publicly accessible to prevent unauthorized access to cached data.
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):
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
When you create a new instance of DatabaseStore, it will:
Check if the specified table (default: 'guardian_cache') exists.
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
Using Cache Stores with Guardian
Once you've constructed a cache store, you can use it when creating a Guardian instance:
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