PHP code example of stellarwp / memoize

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.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stellarwp / memoize example snippets


use StellarWP\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');

use StellarWP\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' ] ] ]

use StellarWP\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:
// []

use StellarWP\Memoize\Memoizer;

$memoizer = new Memoizer();

$memoizer->set('foo', static function () {
    return 'bar';
});

echo $memoizer->get('foo'); // Outputs: bar



declare(strict_types=1);

namespace StellarWP\MyProject;

use StellarWP\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.
 */
class MyProjectClass
{

    private MemoizerInterface $memoizer;

    public function __construct( MemoizerInterface $memoizer )
    {
        $this->memoizer = $memoizer;
    }

    public function get( string $name ): string
    {
        $result = $this->memoizer->get( $name );

        if ( ! $result ) {
            $result = 'some very expensive operation';

            $this->memoizer->set( $name, $result );
        }

        return $result;
    }

    public function delete( string $name ): bool
    {
        $this->memoizer->forget( $name );

        // Run delete operation...

        return true;
    }
}



declare(strict_types=1);

namespace StellarWP\Memoize;

use StellarWP\ContainerContract\ContainerInterface;
use StellarWP\Memoize\Contracts\DriverInterface;
use StellarWP\Memoize\Contracts\MemoizerInterface;
use StellarWP\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 );
 */
final class ServiceProvider
{
    private ContainerInterface $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function register(): void
    {
        $this->container->singleton( DriverInterface::class, MemoryDriver::class );
        $this->container->bind( MemoizerInterface::class, Memoizer::class );
    }
}

use StellarWP\Memoize\Memoizer;
use StellarWP\Memoize\Drivers\MemoryDriver;

$memoizer = new Memoizer(new MemoryDriver());



declare(strict_types=1);

namespace StellarWP\MyProject;

use StellarWP\Memoize\MemoizerInterface;
use StellarWP\Memoize\Traits\MemoizeTrait;

/**
 * An example class inside your project.
 */
class MyProjectClass implements MemoizerInterface
{
    use MemoizeTrait;

    public function find( string $id ): string {
       $result = $this->get( $id );

        if ( ! $result ) {
            $result = 'some very expensive operation';

            $this->set( $id, $result );
        }

        return $result;
    }

    public function delete( string $id ): bool
    {
        $this->forget( $id );

        // Run delete operation...

        return true;
    }
}