Download the PHP package laracraft-tech/memoize without Composer
On this page you can find all versions of the php package laracraft-tech/memoize. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download laracraft-tech/memoize
More information about laracraft-tech/memoize
Files in laracraft-tech/memoize
Package memoize
Short Description Simple PHP trait to memoize function calls.
License MIT
Informations about the package memoize
Memoize
Introduction
This package provides you with a simple PHP trait, which adds high-performance memoization to your classes!
Installation
Usage
The memoize
method accepts a callable
.
No matter how many times you run $myClass->getNumber()
you'll always get the same number.
The memoize
method will only run once per combination of use
variables the closure receives.
So calling $myClass->getNumberForLetter('A')
will always return the same result, but calling $myClass->getNumberForLetter('B')
will return something else.
Some memoization packages uses the arguments of the containing method for the once per combination idea.
We think this feels a bit unintuitive and in certain circumstances will affect performance. So we use the use
variables of the closure as the once per combination key. As a fallback, if you like/need to, we also let you fully self define your once per combination key in a second optional parameter of the closure.
So when calling $myClass->processSomethingHeavy1(SomeModel::find(1))
the variable $relation
will always have the same value like in $myClass->processSomethingHeavy1(SomeModel::find(2))
as long as they have the same foo_relation_id
. In some other memoization packges you would lose performance here, cause the containing method parameter ($someModel) has changed... Note that processSomethingHeavy3
would also lose performance, even though the foo_relation_id
would be the same, cause here also the changed $someModel would be used as the combination key and that model would have at least a different id.
Enable/Disable
You can globally enable or disable the memoize cache by setting MEMOIZATION_GLOBALLY_DISABLED
to true
in your .env
file.
If you only want to disable to memoize cache for a specifiy class, do something like this:
Change log
Please see the changelog for more information on what has changed recently.
Testing
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Zacharias Creutznacher
- All Contributors
License
MIT. Please see the license file for more information.