2017 © Pedro Peláez
 

library closure-cache

Cache method, function or closure results.

image

titasgailius/closure-cache

Cache method, function or closure results.

  • Tuesday, May 16, 2017
  • by TitasGailius
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

ClosureCache

A simple PHP cache mechanism for storing method, function or closure results.\ It calculates result of the closure once and returns it on subsequent calls., (*1)

Usage

<?php

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

Installation

With composer

$ composer require titasgailius/closure-cache
{
    "require": {
        "titasgailius/closure-cache": "~1.00"
    }
}

Example

Before

<?php

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();

After

<?php

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();

ClosureCache is parameter-sensitive

<?php

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');

The Versions

16/05 2017

dev-master

9999999-dev

Cache method, function or closure results.

  Sources   Download

MIT

The Development Requires

by Titas Gailius

cache closure

05/05 2017

1.0

1.0.0.0

Cache method, function or closure results.

  Sources   Download

MIT

The Development Requires

by Titas Gailius

cache closure