Download the PHP package jessegall/laravel-memoize without Composer
On this page you can find all versions of the php package jessegall/laravel-memoize. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-memoize
Laravel Memoize
Laravel Memoize is a library that provides method-level caching for your Laravel Eloquent models and other PHP classes. It improves performance by caching the results of method calls based on their arguments.
Memoization
Memoization is a technique used to cache the results of function calls and return the cached result when the same inputs occur again.
Features
- Easy to implement with a simple trait
- Works with Eloquent models and regular PHP classes
- Supports caching based on method arguments
- Shared cache across multiple instances of the same model
- Automatically invalidates cache on model updates (or custom events)
- Customizable cache drivers for different storage options
- Customizable argument serialization for fine-grained control
Table of Contents
- Installation
- Usage
- Memoization and Models
- Memoize Drivers
- Available Drivers
- Using Drivers
- Creating a Custom Driver
- Switching Drivers Dynamically
- Argument Serialization
- Overriding the Default Factory
- Extending the Default Factory
- Cache
- Clearing Cache
- Customizing Cache Invalidation Events
- Testing
- Contributing
- License
Installation
You can install the package via Composer:
Usage
-
Use the
Memoize
trait in your Eloquent model or PHP class: - Wrap the logic you want to memoize with the
memoize
function:
The result of the callback is memoized based on the arguments provided to the method that calls memoize
.
In this example, the result of expensiveCalculation
is cached based on the values of $param1
and $param2
.
Memoization and Models
When using Laravel Memoize with Eloquent models, here's how it works:
- If you use the
Memoize
trait in a model, the memoization is tied to the model's ID (primary key). - If you pass a model as an argument to a memoized method, the model's ID is used as part of the cache key.
This means:
- Different instances of the same model (same ID) will share the same memoized results.
- Freshly queried models will use existing cached results if available.
Here's how it looks in practice:
Memoize Drivers
Laravel Memoize can use different drivers to store cached results. It comes with two built-in drivers: MemoryDriver
and CacheDriver
.
Available Drivers
-
MemoryDriver (Default): Stores cached results in memory for the duration of the request.
- CacheDriver: Utilizes Laravel's caching system to store memoized results. This allows the results to persist between different requests and to be shared among multiple servers that are connected to the same cache storage.
⚠️ Important Notice for Laravel Octane Users
When using the MemoryDriver
together with Laravel Octane, be aware that memoized results persist across multiple
requests handled by the same worker. This can lead to improved performance but also risks serving stale data. Implement
appropriate cache-clearing mechanisms for frequently changing data to ensure freshness.
Using Drivers
By default, Laravel Memoize uses the MemoryDriver
. To use a different driver, override the memoizeDriver
method in
your model or class:
Creating a Custom Driver
You can create your own custom driver by implementing the DriverInterface
:
-
Create your custom driver class:
- Use your custom driver in your model or class:
Switching Drivers Dynamically
You can switch drivers dynamically based on certain conditions:
Argument Serialization
Laravel Memoize uses an argument serializer to generate unique cache keys based on method arguments. You can customize
this behavior by overriding the default ArgumentSerializerFactoryInterface
or by creating your own implementation of
the ArgumentSerializerFactoryInterface
.
Overriding the Default Factory
To override the default argument serializer factory, you can bind your custom implementation to
the ArgumentSerializerFactoryInterface
in your app service provider:
Extending the Default Factory
If you want to extend the default ArgumentSerializerFactory
to add support for additional types or modify existing
serialization logic, you can create a new class that extends ArgumentSerializerFactory
:
Then, bind your extended serializer in your app service provider:
By customizing the argument serializer, you can control how different types of arguments are serialized for cache key generation, allowing for more fine-grained control over the memoization process.
Cache
Clearing Cache
To manually clear the cache for a specific instance:
Customizing Cache Invalidation Events
By default, the cache of the model is cleared automatically when a model is 'saved' or deleted.
You can customize the events that trigger cache invalidation by overriding the memoizeCacheInvalidationEvents
method:
Testing
To run the tests, use the following command:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This package is open-sourced software licensed under the MIT license.