Download the PHP package stellarwp/memoize without Composer
On this page you can find all versions of the php package stellarwp/memoize. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download stellarwp/memoize
More information about stellarwp/memoize
Files in stellarwp/memoize
Package memoize
Short Description A flexible memoization library for memory caching.
License GPL-2.0
Informations about the package memoize
Stellar Memoize
A flexible memoization library for memory caching.
Table of Contents
- Memoization
- Installation
- Notes on examples
- Usage
- Simple example
- Setting a nested structure
- Purging a nested structure
- Caching with closures
- Using with a dependency injection container
- Drivers
- MemoryDriver
- Traits
Memoization
Memoization is the process of caching the results of expensive function calls so that they can be reused when the same inputs occur again.
Installation
It's recommended that you install Memoize as a project dependency via Composer:
We actually recommend that this library gets included in your project using Strauss.
Luckily, adding Strauss to your
composer.json
is only slightly more complicated than adding a typical dependency, so checkout our strauss docs.
Notes on examples
All namespaces within the examples will be using the StellarWP\Memoize
, however, if you are using Strauss, you will need to prefix these namespaces with your project's namespace.
Usage
Simple example
Setting a nested structure
Memoize allows you to use dot notation to set, get, and forget values from a nested structure. This allows you to easily add/fetch values and then purge individual items or whole collections of items.
Purging a nested structure
Caching with closures
Memoize also supports using closures as values that get resolved before setting in the cache.
Using with a dependency injection container
Project class
Service Provider
Drivers
Memoize comes with a single driver out of the box, but you can also create your own using the StellarWP\Memoize\Contracts\DriverInterface
.
MemoryDriver
The MemoryDriver
is a simple in-memory driver. Basically, contains an array property in the driver that holds the memoized values. You can manually specify the use of this driver or any other driver like so:
Traits
The MemoizeTrait
provides a convenient way to add memoization to your classes, ensuring that cached results are isolated to each instance,
preventing cross-talk or key collisions between different objects. Ideal for enhancing existing classes with lightweight, instance-specific caching.
💡 The MemoizeTrait uses in-memory storage only and cannot be changed.