bit-mx/cache-entities

Creates cacheable entities

1.0.2 2024-05-02 23:05 UTC

This package is auto-updated.

Last update: 2024-05-02 23:06:01 UTC


README

Manage your cache easily with Cache Entities.

Table of Contents

Introduction

Cache Entities is a package that allows you to manage your cache using a simple and clean API.

Installation

You can install the package via composer:

composer require bit-mx/cache-entities

Compatibility

This package is compatible with Laravel 10.x and above.

Due laravel 11 requires php 8.2, this package is compatible with php 8.2 and above.

Getting Started

Create a Cache Entity class

To create a Cache Entity, you need to extend the CacheEntity class and implement the resolveKey, resolveTtl, and resolveValue methods.

namespace App\CacheEntities;

use BitMx\CacheEntities\CacheEntity;
use App\Models\User;
use Carbon\CarbonInterval;

class CurrentUserCache extends CacheEntity
{
    public function __construct(
        protected int $id,
    )
    {
    }

    protected function resolveKey(): string
    {
        return sprintf('current_user:%s', $this->id);
    }

    protected function resolveTtl(): CarbonInterval
    {
        return CarbonInterval::days(12);
    }

    protected function resolveValue(): mixed
    {
        return User::find($this->id);
    }
}

You can use the artisan command to create a new Cache Entity:

php artisan make:cache-entity CurrentUserCacheEntity

This command will create a new Cache Entity in the app/cacheEntities directory.

Driver

You can set the driver name overriding the resolveCacheStore method.

namespace App\CacheEntities;

use BitMx\CacheEntities\CacheEntity;
use App\Models\User;
use Carbon\CarbonInterval;

class CurrentUserCache extends CacheEntity
{
    
    protected function resolveCacheStore(): string
    {
        return 'redis';
    }
}

Get the cached value

To get the cached value, you can use the get method.

use App\CacheEntities\CurrentUserCacheEntity;

$cacheEntity = new CurrentUserCacheEntity(1);

$user = $cacheEntity->get();

Helper methods

You can use the following helper methods to work with the cache:

exists

The exists method returns true if the cache key exists, and false otherwise.

use App\CacheEntities\CurrentUserCacheEntity;

$cacheEntity = new CurrentUserCacheEntity(1);

$user = $cacheEntity->get();

if ($cacheEntity->exists()) {
    // The cache key exists
} else {
    // The cache key does not exist
}

doesNotExist

The doesNotExist method returns true if the cache key does not exist, and false otherwise.

use App\CacheEntities\CurrentUserCacheEntity;

$cacheEntity = new CurrentUserCacheEntity(1);

$user = $cacheEntity->get();

if ($cacheEntity->doesNotExist()) {
    // The cache key does not exist
} else {
    // The cache key exists
}

forget

The forget method removes the cache key.

use App\CacheEntities\CurrentUserCacheEntity;

$cacheEntity = new CurrentUserCacheEntity(1);

$user = $cacheEntity->get();

$cacheEntity->forget();