superbalist/laravel4-psr6-cache-bridge

A PSR6 cache implementation for Laravel 4

1.0.1 2017-07-04 07:00 UTC

This package is auto-updated.

Last update: 2024-04-11 23:14:24 UTC


README

A PSR6 cache implementation for Laravel 4.

Author Build Status StyleCI Software License Packagist Version Total Downloads

This library is based off the Laravel 5 implementation by madewithlove/illuminate-psr-cache-bridge.

Installation

composer require superbalist/laravel4-psr6-cache-bridge

Register the service provider in app.php

'providers' => [
    // ...
    'Superbalist\Laravel4PSR6CacheBridge\ServiceProvider'
]

Usage

You can now start using or injecting the CacheItemPoolInterface implementation for libraries which expect a PSR6 cache implementation.

use DateTimeImmutable;
use Psr\Cache\CacheItemPoolInterface;
use Superbalist\Laravel4PSR6CacheBridge\LaravelCacheItem::class;
use Superbalist\Laravel4PSR6CacheBridge\LaravelCacheItemPool::class;

$pool = app(CacheItemPoolInterface::class);
// or
$pool = app(LaravelCacheItemPool::class);

// save an item with an absolute ttl
$item = new LaravelCacheItem('first_name', 'Bob', true);
$item->expiresAt(new DateTimeImmutable('2017-06-30 14:30:00'));
$pool->save($item);

// save an item with a relative ttl
$item = new LaravelCacheItem('first_name', 'Bob', true);
$item->expiresAfter(60);
$pool->save($item);

// save an item permanently
$item = new LaravelCacheItem('first_name', 'Bob', true);
$pool->save($item);

// retrieve an item
$item = $pool->get('first_name');

// working with an item
var_dump($item->getKey());
var_dump($item->get());
var_dump($item->isHit());

// retrieve one or many items
$items = $pool->getItems(['first_name']);
var_dump($items['first_name']);

// check if an item exists in cache
var_dump($pool->hasItem('first_name'));

// wipe out all items
$pool->clear();

// delete an item
$pool->deleteItem('first_name');

// delete one or many items
$pool->deleteItems(['first_name']);

// save a deferred item
$item = new LaravelCacheItem('first_name', 'Bob', true);
$item->expiresAt(new DateTimeImmutable('+1 hour'));
$pool->saveDeferred($item);

// commit all deferred items
$pool->commit();