Coder Social home page Coder Social logo

in-memory-cache-php's Introduction

PSR-6 In-Memory Cache

A PSR-6 In-Memory cache that can be used as a default implementation and in tests.

Current version Packagist PHP Version Support Monthly Downloads Total Downloads Tests

Installation

composer require beste/in-memory-cache

Usage

use Beste\Cache\InMemoryCache;

$cache = new InMemoryCache();

$item = $cache->getItem('key');

assert($item->isHit() === false);
assert($item->get() === null);

$item->set('value');
$cache->save($item);

// Later...

$item = $cache->getItem('key');

assert($item->isHit() === true);
assert($item->get() === 'value');

You can also provide your own PSR-20 clock implementation, for example a frozen clock for testing, for example from the beste/clock library.

use Beste\Clock\FrozenClock;
use Beste\Cache\InMemoryCache;

$clock = FrozenClock::fromUTC()
$cache = new InMemoryCache();

$item = $cache->getItem('key');
$item->set('value')->expiresAfter(new DateInterval('PT5M'));
$cache->save($item);

$clock->setTo($clock->now()->add(new DateInterval('PT2M')));
assert($cache->getItem('key')->isHit() === true);

$clock->setTo($clock->now()->add(new DateInterval('PT5M')));
assert($cache->getItem('key')->isHit() === false);

Running tests

composer test

License

This project is published under the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.