Download the PHP package bit-mx/cache-entities without Composer
On this page you can find all versions of the php package bit-mx/cache-entities. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package cache-entities
Cache Entities
Manage your cache easily with Cache Entities.
Table of Contents
- Introduction
- Installation
- Compatibility
- Getting Started
- Create a Cache Entity class
- Driver
- Get the cached value
- Helper methods
- exists
- doesNotExist
- forget
- Memoization
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:
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.
You can use the artisan command to create a new Cache Entity:
This command will create a new Cache Entity in the app/cacheEntities directory.
Driver
You can set the driver name overriding the resolveCacheStore method.
Get the cached value
To get the cached value, you can use the get method.
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.
doesNotExist
The doesNotExist method returns true if the cache key does not exist, and false otherwise.
forget
The forget method removes the cache key.
Memoization
By default, Cache Entities do not use memoization. However, you can enable it by using the HasMemoization trait in your Cache Entity class.
When memoization is enabled, the cache entity will use Laravel's memo() method to store the cached value in memory during the request lifecycle. This prevents redundant cache lookups for the same key within a single request, improving performance.
Enabling Memoization
To enable memoization, simply add the HasMemoization trait to your Cache Entity class:
With memoization enabled:
- The first call to
get()will fetch the value from cache or executeresolveValue() - Subsequent calls to
get()within the same request will return the memoized value from memory - This avoids redundant cache lookups, improving performance when accessing the same cache entity multiple times
Without Memoization
If you don't use the HasMemoization trait, each call to get() will perform a cache lookup, even within the same request.