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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-memoize

Laravel Memoize

Latest Version on Packagist

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


Table of Contents

Installation

You can install the package via Composer:

Usage

  1. Use the Memoize trait in your Eloquent model or PHP class:

  2. 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:

This means:

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

  1. MemoryDriver (Default): Stores cached results in memory for the duration of the request.

  2. 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:

  1. Create your custom driver class:

  2. 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.


All versions of laravel-memoize with dependencies

PHP Build Version
Package Version
Requires laravel/framework Version ^10.0|^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package jessegall/laravel-memoize contains the following files

Loading the files please wait ....