Git Product home page Git Product logo

varcache's Introduction

jasny-banner

VarCache

Build Status Scrutinizer Code Quality Code Coverage Packagist Stable Version Packagist License

A cache implementation based on 500x faster caching and brick/varexporter.

Installation

composer require jasny/varcache

Usage

PSR-16

The Cache object implements PSR-16 simple cache.

use Jasny\VarCache\Cache;

$cache = new Cache('path/to/cache/dir');

$service = $cache->get('service');

if ($service === null) {
    $service = new Service();

    // ... Initialize service

    $cache->set('service', $service);
}

Caveat: Method has() disregards the ttl.

Resolve

The resolve method takes a closure (without arguments). It will cache the return value of the function as PHP script. On subsequent requests the closure isn't called, but the cached value is returned.

use Jasny\VarCache\Cache;

$cache = new Cache('path/to/cache/dir');

$service = $cache->resolve(function() {
    $service = new Service();
    
    // ... Initialize service
    
    return $service;
});

The cache function uses reflection to create a hash of the closure. Limited information about the closure is returned through reflection. This means that changes to the code don't automatically result in a different hash.

Null object

During development, you may not want to cache. NoCache is a null object that will not store anything.

use Jasny\VarCache\NoCache;

$cache = new NoCache();

$service = $cache->resolve(function() {
    $service = new Service();
    
    // ... Initialize service
    
    return $service;
});

varcache's People

Contributors

jasny avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mattymaloney

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.