1. Go to this page and download the library: Download stellarwp/memoize library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stellarwp / memoize example snippets
useStellarWP\Memoize\Memoizer;
$memoizer = new Memoizer();
$memoizer->set('foo', 'bar');
if ($memoizer->has('foo')) {
echo $memoizer->get('foo'); // Outputs: bar
}
// Unsets foo from the memoization cache.
$memoizer->forget('foo');
useStellarWP\Memoize\Memoizer;
$memoizer = new Memoizer();
$memoizer->set('foo.bar.bork', 'baz');
// This results in the following cache:// [// 'foo' => [// 'bar' => [// 'bork' => 'baz',// ],// ],// ]// You can fetch the value like so:
$value = $memoizer->get('foo.bar.bork');
echo $value; // Outputs: baz// You can fetch anywhere up the chain:
$value = $memoizer->get('foo.bar');
echo $value; // Outputs: [ 'bork' => 'baz' ]
$value = $memoizer->get('foo');
echo $value; // Outputs: [ 'bar' => [ 'bork' => 'baz' ] ]
$value = $memoizer->get();
echo $value; // Outputs: [ 'foo' => [ 'bar' => [ 'bork' => 'baz' ] ] ]
useStellarWP\Memoize\Memoizer;
$memoizer = new Memoizer();
$memoizer->set('foo.bar.bork', 'baz');
$memoizer->forget('foo.bar.bork');
// This results in the following cache:// [// 'foo' => [// 'bar' => [],// ],// ]
$memoizer->forget('foo.bar');
// This results in the following cache:// [// 'foo' => [],// ]
$memoizer->forget('foo');
// This results in the following cache:// []
$memoizer->forget();
// This results in the following cache:// []
useStellarWP\Memoize\Memoizer;
$memoizer = new Memoizer();
$memoizer->set('foo', staticfunction(){
return'bar';
});
echo $memoizer->get('foo'); // Outputs: bar
declare(strict_types=1);
namespaceStellarWP\MyProject;
useStellarWP\Memoize\MemoizerInterface;
// Dependencies automatically auto-wired due to the definitions in ServiceProvider.php via// $this->container->get( MyProjectClass::class )/**
* An example class inside your project using the Memoize library.
*/classMyProjectClass{
private MemoizerInterface $memoizer;
publicfunction__construct( MemoizerInterface $memoizer ){
$this->memoizer = $memoizer;
}
publicfunctionget( string $name ): string{
$result = $this->memoizer->get( $name );
if ( ! $result ) {
$result = 'some very expensive operation';
$this->memoizer->set( $name, $result );
}
return $result;
}
publicfunctiondelete( string $name ): bool{
$this->memoizer->forget( $name );
// Run delete operation...returntrue;
}
}
declare(strict_types=1);
namespaceStellarWP\Memoize;
useStellarWP\ContainerContract\ContainerInterface;
useStellarWP\Memoize\Contracts\DriverInterface;
useStellarWP\Memoize\Contracts\MemoizerInterface;
useStellarWP\Memoize\Drivers\MemoryDriver;
/**
* Container ServiceProvider to tell the DI Container how to build everything when another
* instance is requested from the Container that uses our interface.
*
* @example $this->container->get( MyProjectClass::class );
*/finalclassServiceProvider{
private ContainerInterface $container;
publicfunction__construct(ContainerInterface $container){
$this->container = $container;
}
publicfunctionregister(): void{
$this->container->singleton( DriverInterface::class, MemoryDriver::class );
$this->container->bind( MemoizerInterface::class, Memoizer::class );
}
}
useStellarWP\Memoize\Memoizer;
useStellarWP\Memoize\Drivers\MemoryDriver;
$memoizer = new Memoizer(new MemoryDriver());
declare(strict_types=1);
namespaceStellarWP\MyProject;
useStellarWP\Memoize\MemoizerInterface;
useStellarWP\Memoize\Traits\MemoizeTrait;
/**
* An example class inside your project.
*/classMyProjectClassimplementsMemoizerInterface{
useMemoizeTrait;
publicfunctionfind( string $id ): string{
$result = $this->get( $id );
if ( ! $result ) {
$result = 'some very expensive operation';
$this->set( $id, $result );
}
return $result;
}
publicfunctiondelete( string $id ): bool{
$this->forget( $id );
// Run delete operation...returntrue;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.