Creating your own cache driver

Guardian allows you to create and use custom cache drivers to suit your specific needs. This guide will walk you through the process of creating your own cache driver.

Step 1: Implement the CacheInterface

Your custom cache driver must implement the Midnite81\Guardian\Contracts\Store\CacheInterface. This interface defines the methods that your cache driver needs to implement:

namespace Midnite81\Guardian\Contracts\Store;

use DateInterval;
use DateTimeInterface;

interface CacheInterface
{
    public function get(string $key, mixed $default = null): mixed;
    public function has(string $key): bool;
    public function put(string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool;
    public function forget(string $key): bool;
}

Step 2: Create Your Custom Cache Driver

Create a new class that implements the CacheInterface. Here's an example skeleton:

namespace YourNamespace;

use DateInterval;
use DateTimeInterface;
use Midnite81\Guardian\Contracts\Store\CacheInterface;

class YourCustomCacheDriver implements CacheInterface
{
    public function get(string $key, mixed $default = null): mixed
    {
        // Implement get logic
    }

    public function has(string $key): bool
    {
        // Implement has logic
    }

    public function put(string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
    {
        // Implement put logic
    }

    public function forget(string $key): bool
    {
        // Implement forget logic
    }
}

Step 3: Implement the Required Methods

Fill in the logic for each method based on your caching mechanism. Here's a brief description of what each method should do:

  • get: Retrieve a value from the cache by its key. Return the default value if the key doesn't exist.

  • has: Check if a key exists in the cache.

  • put: Store a value in the cache with an optional TTL (Time To Live).

  • forget: Remove a value from the cache by its key.

Step 4: Use Your Custom Cache Driver

To use your custom cache driver with Guardian, pass an instance of your driver when creating a Guardian instance:

use Midnite81\Guardian\Guardian;
use YourNamespace\YourCustomCacheDriver;

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

Best Practices

  1. Error Handling: Implement proper error handling in your cache driver. You may want to create a custom exception class for cache-related errors.

  2. Serialization: Consider serializing and deserializing complex data types when storing and retrieving from the cache.

  3. TTL Handling: Implement proper handling of the TTL parameter in the put method. This may involve converting DateInterval and DateTimeInterface to seconds.

  4. Performance: Ensure your cache driver is optimized for performance, especially if you're dealing with high-traffic applications.

  5. Testing: Write unit tests for your custom cache driver to ensure it behaves correctly and integrates well with Guardian.

By following these steps and best practices, you can create a custom cache driver that integrates seamlessly with the Guardian package and meets your specific caching requirements.

Last updated