PHP code example of tonybogdanov / memoize

1. Go to this page and download the library: Download tonybogdanov/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/ */

    

tonybogdanov / memoize example snippets


class ClassUsingCaching {
    use \TonyBogdanov\Memoize\Traits\MemoizeTrait;

    public function getObjectLevelCachedThing() {
        return $this->memoize( __METHOD__, function () {
            return 'thing'; // heavy code that needs to run only once per object instance.
        } );
    }
}

$object->unmemoize( 'key' );

$object->isMemoized( 'key' );

class ClassUsingCaching {
    use \TonyBogdanov\Memoize\Traits\MemoizeTrait;

    public static function getClassLevelCachedThing() {
        return static::memoizeStatic( __METHOD__, function () {
            return 'thing'; // heavy code that needs to run only once per class.
        } );
    }
}

StaticClass::unmemoizeStatic( 'key' );

StaticClass::isMemoizedStatic( 'key' );

// per-object
$this->memoizeForeign( $object, 'key', 'value' );
$this->unmemoizeForeign( $object, 'key' );
$this->isMemoizedForeign( $object, 'key' );

// per-class
StaticClass::memoizeStaticForeign( AnotherStaticClass::class, 'key', 'value' );
StaticClass::unmemoizeStaticForeign( AnotherStaticClass::class, 'key' );
StaticClass::isMemoizedStaticForeign( AnotherStaticClass::class, 'key' );

Memoize::enable();
Memoize::disable();