PHP code example of titasgailius / closure-cache

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

    

titasgailius / closure-cache example snippets




use ClosureCache\ClosureCache;

function somethingSlow()
{
    return once(function () {
        // Code...
    });
}




use ClosureCache\ClosureCache;

class SomeClass
{
    public static function think()
    {
        sleep(10);

        return 'It takes some time to process this.';
    }
}

/**
 * 10 seconds to process it.
 */
SomeClass::think();

/**
 * Another 10 seconds to process it
 * which makes it 20 seconds in total.
 */
SomeClass::think();



use ClosureCache\ClosureCache;

class SomeClass
{
    public static function think()
    {
        return once(function () {
            sleep(10);

            return 'It takes some time to process this.';
        });
    }
}

/**
 * 10 seconds to process
 */
SomeClass::think();

/**
 * ClosureCache detects that this was already
 * processed and returns it from the cache.
 */
SomeClass::think();



use ClosureCache\ClosureCache;

class SomeClass
{
    public static function think($message)
    {
        return once(function () use ($message) {
            sleep(10);

            return $message;
        });
    }
}

/**
 * 10 seconds to process
 */
SomeClass::think('foo');

/**
 * Another 10 seconds to process it because
 * different parameters were passed.
 */
SomeClass::think('bar');