Coder Social home page Coder Social logo

stash's Introduction

Stash

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Cache storage system

Stash provides a PSR6 / PSR16 compatible cache system for PHP.

Get news and updates on the DecodeLabs blog.


Installation

composer require decodelabs/stash

Usage

Store and access data in a standardised volatile cache either via the PSR6 or PSR16 interface mechanisms. Caches are namespaced to allow for clean separation of data between usage domains.

use DecodeLabs\Stash;

$myCache = Stash::get('MyCache');

if(!$cache->has('myValue')) {
    $cache->set('myValue', [1, 2, 3]);
}

$total = 0;

foreach($cache->get('myValue', []) as $number) {
    $total += $number;
}

$cache->delete('myValue');

Fetch

Use the fetch method to ensure a cache value is in place in one call:

$myValue = $myCache->fetch('myValue', function() {
    return [1, 2, 3]; // Only called if key not found in cache
});

Array Access

Array access methods provide quick offset access to cache data:

if(!isset($myCache['myValue'])) {
    $myCache['myValue'] = 'Hello world';
}

echo $myCache['myValue'];
unset($MyCache['myValue']);

Object access

Object access works the same way as ArrayAccess, but with the PSR6 Cache Item object as the return:

$item = $myCache->myValue;

if(!$item->isHit()) {
    $item->set('Hello world');
}

echo $item->get();
$item->delete();

Drivers

The following drivers are available out of the box:

  • Memcache
  • Redis
  • Predis (native PHP redis client)
  • APCu
  • File (serialized data)
  • PhpFile (var_export data)
  • PhpArray (in memory)
  • Blackhole (nothing stored)

However, Stash uses Archetype to load driver classes so additional drivers may be provided by implementing your own Resolver.

By default, Stash will use the best-fit driver for your environment, starting with Memcache, through Redis and APCu, and falling back on the File store.

Configuration

All drivers have default configuration to allow them to work out of the box, however Stash provides the ability to implement your own configuration loader so that you can control drivers and settings on a per-namespace basis

Implement the following interface however your system requires; all nullable methods can just return null to use default configuration:

interface Config
{
    public function getDriverFor(string $namespace): ?string;
    public function isDriverEnabled(string $driver): bool;
    public function getAllDrivers(): array;
    public function getDriverSettings(string $driver): ?array;

    public function getPileUpPolicy(string $namespace): ?PileUpPolicy;
    public function getPreemptTime(string $namespace): ?int;
    public function getSleepTime(string $namespace): ?int;
    public function getSleepAttempts(string $namespace): ?int;
}

Then tell Stash about your configuration provider:

Stash::setConfig(new MyConfig());

Custom Store methods

By default, newly loaded caches use a generic Store implementation, however if you require custom methods for domain-oriented data access, you can implement your own Store classes using a custom Archetype Resolver.

namespace MyApp;

use DecodeLabs\Archetype;
use DecodeLabs\Archetype\Resolver\Extension as ArchetypeExtension;
use DecodeLabs\Stash\Store;
use DecodeLabs\Stash\Store\Generic;

class MyCache extends Generic
{

    public function getMyData(): string
    {
        return $this->fetch('myData', function() {
            return 'Hello world';
        });
    }
}

Archetype::register(new ArchetypeExtension(
    Store::class, namespace::class
))

Licensing

Stash is licensed under the MIT License. See LICENSE for the full license text.

stash's People

Contributors

betterthanclay avatar

Watchers

 avatar  avatar  avatar

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.