An improved helper for working with cache

Overview

Laravel Cache

Laravel Cache

Stable Version Unstable Version Total Downloads License

Installation

To get the latest version of Laravel Cache, simply require the project using Composer:

$ composer require dragon-code/laravel-cache

Or manually update require block of composer.json and run composer update.

{
    "require": {
        "dragon-code/laravel-cache": "^2.0"
    }
}

Using

Keys And Tags

In addition to passing an explicit value, you can also pass objects and arrays to the keys and tags methods.

For example:

use DragonCode\Cache\Services\Cache;
use DragonCode\SimpleDataTransferObject\DataTransferObject;

$arr1 = ['foo', 'bar']
$arr2 = new ArrayObject(['foo', 'bar'])
$arr3 = DataTransferObject::make(['foo', 'bar'])

Cache::make()->key($arr1)->tags($arr1);
Cache::make()->key($arr2)->tags($arr3);
Cache::make()->key($arr2)->tags($arr3);

Cache::make()->key([$arr1, $arr2, $arr3, 'foo', 'bar'])->tags([$arr1, $arr2, $arr3, 'foo', 'bar']);
Cache::make()->key([$arr1, $arr2, $arr3, 'foo', 'bar'])->tags([$arr1, $arr2, $arr3, 'foo', 'bar']);
Cache::make()->key([$arr1, $arr2, $arr3, 'foo', 'bar'])->tags([$arr1, $arr2, $arr3, 'foo', 'bar']);

Keys Handling

Since the main problem of working with the cache's key compilation, this package solves it.

By passing values to the keys method, we get a ready-made key at the output.

For example:

use DragonCode\Cache\Services\Cache;

$cache = Cache::make()->key('foo', 'bar', [null, 'baz', 'baq']);

// Key is `acbd18db4cc2f85cedef654fccc4a4d8:37b51d194a7513e45b56f6524f2d51f2:73feffa4b7f6bb68e44cf984c85f6e88:b47951d522316fdd8811b23fc9c2f583`

This means that when writing to the cache, the tree view will be used.

For example:

use DragonCode\Cache\Services\Cache;

Cache::make()->key('foo', 'foo')->put('foo');
Cache::make()->key('foo', 'bar')->put('bar');
Cache::make()->key('baz')->put('baz');

// acbd18db4cc2f85cedef654fccc4a4d8:
//     acbd18db4cc2f85cedef654fccc4a4d8: foo
//     37b51d194a7513e45b56f6524f2d51f2: bar
// 73feffa4b7f6bb68e44cf984c85f6e88: baz

When Enabled

Basic

By default, the cache will be written for 1 day.

use DragonCode\Cache\Services\Cache;

$cache = Cache::make()->key('foo', 'bar', ['baz', 'baq']);

$cache->put(static fn() => 'Some value');
// or
$cache->put('Some value');
// Contains cached `Some value`

$cache->get();
// Returns cached `Some value`

$cache->has();
// Returns `true`

$cache->forget();
// Will remove the key from the cache.

Custom TTL

The cache will be written for the specified number of minutes.

use DragonCode\Cache\Services\Cache;

$cache = Cache::make()
    ->ttl($minutes)
    ->key('foo', 'bar', ['baz', 'baq']);

$cache->put(static fn() => 'Some value');
// or
$cache->put('Some value');
// Contains cached `Some value`

$cache->get();
// Returns cached `Some value`

$cache->has();
// Returns `true`

$cache->forget();
// Will remove the key from the cache.

Tagged

For repositories that support tagging, the keys will be saved separated by tags.

use DragonCode\Cache\Services\Cache;

$cache = Cache::make()
    ->tags('actor', 'author')
    ->key('foo', 'bar', ['baz', 'baq']);

$cache->put(static fn() => 'Some value');
// or
$cache->put('Some value');
// Contains cached `Some value`

$cache->get();
// Returns cached `Some value`

$cache->has();
// Returns `true`

$cache->forget();
// Will remove the key from the cache.

To retrieve a tagged cache item, pass the same ordered list of tags to the tags method and then call the get method with the key you wish to retrieve:

use DragonCode\Cache\Services\Cache;

$cache = Cache::make()->key('foo', 'bar');

$cache->tags('actor', 'author')->put(static fn() => 'Some value');
// or
$cache->tags('actor', 'author')->put('Some value');
// Contains cached `Some value`

$cache->tags('actor', 'author')->get();
// Returns cached `Some value`

$cache->tags('actor')->get();
// Returns `null`

$cache->tags('author')->get();
// Returns `null`

See the official Laravel documentation.

When Disabled

Passing when = false will not write to the cache.

use DragonCode\Cache\Services\Cache;

$cache = Cache::make()
    ->when(false)
    ->key('foo', 'bar');

$value = $cache->put(static fn() => 'Some value');
// or
$value = $cache->put('Some value');
// Returns `Some value`

$cache->get();
// Returns `null`

$cache->has();
// Returns `false`

License

This package's licensed under the MIT License.

Comments
Releases(v3.2.0)
  • v3.2.0(Oct 7, 2022)

    What's Changed

    • Added key generation by Carbon object by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/59

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v3.1.0...v3.2.0

    Source code(tar.gz)
    Source code(zip)
  • v3.1.0(Aug 26, 2022)

    What's Changed

    • Added constants for TTL: day, week and month by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/56

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v3.0.0...v3.1.0

    Source code(tar.gz)
    Source code(zip)
  • v3.0.0(Apr 20, 2022)

    What's Changed

    • Bump dragon-code/support 6.1 by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/47
    • Bump PHP 8.0+ support by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/47

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.13.0...v3.0.0

    Source code(tar.gz)
    Source code(zip)
  • v2.13.0(Feb 24, 2022)

    What's Changed

    • Fixed put method. Before that, it was an alias for remember. by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/43

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.12.1...v2.13.0

    Source code(tar.gz)
    Source code(zip)
  • v2.12.1(Feb 18, 2022)

    What's Changed

    • Fixed object serialization bug by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/42

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.12.0...v2.12.1

    Source code(tar.gz)
    Source code(zip)
  • v2.12.0(Feb 15, 2022)

    What's Changed

    • Replace dragon-code/simple-data-transfer-object with dragon-code/simple-dto by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/41

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.11.2...v2.12.0

    Source code(tar.gz)
    Source code(zip)
  • v2.11.2(Feb 4, 2022)

    What's Changed

    • Fixed handling of dynamic classes by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/36
    • Docs and test updated by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/37

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.11.1...v2.11.2

    Source code(tar.gz)
    Source code(zip)
  • v2.11.1(Feb 1, 2022)

    What's Changed

    • Added tests by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/34
    • Removed zero filtering (0) by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/35

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.11.0...v2.11.1

    Source code(tar.gz)
    Source code(zip)
  • v2.11.0(Jan 20, 2022)

    What's Changed

    • Added remember method - it's a alias for improve usability by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/33

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.10.0...v2.11.0

    Source code(tar.gz)
    Source code(zip)
  • v2.10.0(Jan 18, 2022)

    What's Changed

    • Added Laravel 9 support by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/32

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.9.1...v2.10.0

    Source code(tar.gz)
    Source code(zip)
  • v2.9.1(Dec 16, 2021)

    What's Changed

    • Update README.md by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/29
    • Small refactoring by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/30
    • Functionality of the ttlBy method has been moved to ttl 😁 by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/31

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.9.0...v2.9.1

    Source code(tar.gz)
    Source code(zip)
  • v2.9.0(Dec 16, 2021)

    What's Changed

    • Added the ability to split TTL settings into minutes and seconds by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/27
    • Added the ability to use a TTL contract by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/28

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.8.0...v2.9.0

    Source code(tar.gz)
    Source code(zip)
  • v2.8.0(Dec 16, 2021)

    What's Changed

    • Added the ability to store all TTLs in one place 😎 by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/26

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.7.0...v2.8.0

    Source code(tar.gz)
    Source code(zip)
  • v2.7.0(Dec 14, 2021)

    What's Changed

    • Added the ability to pass third-party objects as a parameter by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/25

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.6.0...v2.7.0

    Source code(tar.gz)
    Source code(zip)
  • v2.6.0(Dec 14, 2021)

    What's Changed

    • Added the ability to set TTL in seconds by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/24

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.5.1...v2.6.0

    Source code(tar.gz)
    Source code(zip)
  • v2.5.1(Dec 14, 2021)

  • v2.5.0(Dec 14, 2021)

    What's Changed

    • Normalize composer.json by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/20
    • Split GitHub Actions by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/21
    • Added doesntHave method by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/22
    • Update GitHub Actions by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/23

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.4.0...v2.5.0

    Source code(tar.gz)
    Source code(zip)
  • v2.4.0(Nov 23, 2021)

    What's Changed

    • Allow a Closure to be Passed as a TTL in Cache ttl() method by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/19

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.3.0...v2.4.0

    Source code(tar.gz)
    Source code(zip)
  • v2.3.0(Nov 17, 2021)

    What's Changed

    • Added the ability to pass an array object to keys and tags methods by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/18

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.2.0...v2.3.0

    Source code(tar.gz)
    Source code(zip)
  • v2.2.0(Nov 17, 2021)

    What's Changed

    • Added processing of array objects by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/12
    • Added dragon-code/simple-data-transfer-object package compatibility… by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/13
    • Changed behavior when caching is disabled by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/14

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.1.1...v2.2.0

    Source code(tar.gz)
    Source code(zip)
  • v2.1.1(Nov 16, 2021)

    What's Changed

    • Removed the CacheManager facade by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/11

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.1.0...v2.1.1

    Source code(tar.gz)
    Source code(zip)
  • v2.1.0(Nov 16, 2021)

    What's Changed

    • [2.x] Added tests by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/9
    • [2.x] Added the ability to pass non-callable values by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/10

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.0.1...v2.1.0

    Source code(tar.gz)
    Source code(zip)
  • v2.0.1(Nov 16, 2021)

    What's Changed

    • [2.x] Fixed md5() expects parameter 1 to be string, int given by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/5
    • [2.x] Added tests for keys and tags by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/6
    • [2.x] Removed code duplication by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/7
    • [2.x] Fixed InvalidArgumentException: Cache store [] is not defined. by @andrey-helldar in https://github.com/TheDragonCode/laravel-cache/pull/8

    Full Changelog: https://github.com/TheDragonCode/laravel-cache/compare/v2.0.0...v2.0.1

    Source code(tar.gz)
    Source code(zip)
Owner
The Dragon Code
The Dragon Code
The place to keep your cache.

Stash - A PHP Caching Library Stash makes it easy to speed up your code by caching the results of expensive functions or code. Certain actions, like d

Tedious Developments 944 Jan 4, 2023
PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APC(u), SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

Donate/Support: Documentation: https://www.scrapbook.cash - API reference: https://docs.scrapbook.cash Table of contents Installation & usage Adapters

Matthias Mullie 295 Nov 28, 2022
Cache slam defense using a semaphore to prevent dogpile effect.

metaphore PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates, stampending herd or Slashdot effect). Problem: t

Przemek Sobstel 102 Sep 28, 2022
:zap: Simple Cache Abstraction Layer for PHP

⚡ Simple Cache Class This is a simple Cache Abstraction Layer for PHP >= 7.0 that provides a simple interaction with your cache-server. You can define

Lars Moelleken 27 Dec 8, 2022
Doctrine Cache component

Doctrine Cache Cache component extracted from the Doctrine Common project. Documentation This library is deprecated and will no longer receive bug fix

Doctrine 7.6k Jan 3, 2023
LRU Cache implementation in PHP

PHP LRU Cache implementation Intro WTF is a LRU Cache? LRU stands for Least Recently Used. It's a type of cache that usually has a fixed capacity and

Rogério Vicente 61 Jun 23, 2022
Simple cache abstraction layer implementing PSR-16

sabre/cache This repository is a simple abstraction layer for key-value caches. It implements PSR-16. If you need a super-simple way to support PSR-16

sabre.io 48 Sep 9, 2022
PSR-6 cache implementation adapting a given PSR-16 instance

PSR-6 cache implementation adapting PSR-16 This package provides a PSR-6 cache instance when you only have a PSR-16 cache at hand. As PSR-6 is more fe

null 1 Oct 15, 2021
More Than Just a Cache: Redis Data Structures

More Than Just a Cache: Redis Data Structures Redis is a popular key-value store, commonly used as a cache or message broker service. However, it can

Andy Snell 2 Oct 16, 2021
Simple cache

Simple cache

Róbert Kelčák 3 Dec 17, 2022
Elephant - a highly performant PHP Cache Driver for Kirby 3

?? Kirby3 PHP Cache-Driver Elephant - a highly performant PHP Cache Driver for Kirby 3 Commerical Usage Support open source! This plugin is free but i

Bruno Meilick 11 Apr 6, 2022
Zend Framework cache backend for MongoDB

Zend_Cache_Backend_Mongo Author: Anton Stöckl About Zend_Cache_Backend_Mongo is a Zend Framework Cache Backend for MongoDB. It supports tags and autoc

Anton Stöckl 12 Feb 19, 2020
PHP local cache

__ ____ _________ ______/ /_ ___ / __ \/ ___/ __ `/ ___/ __ \/ _ \ / /_/ / /__/ /_/ / /__/ / / / __/ / ._

Jayden Lie 48 Sep 9, 2022
A fast, lock-free, shared memory user data cache for PHP

Yac is a shared and lockless memory user data cache for PHP.

Xinchen Hui 815 Dec 18, 2022
A simple cache library. Implements different adapters that you can use and change easily by a manager or similar.

Desarolla2 Cache A simple cache library, implementing the PSR-16 standard using immutable objects. Caching is typically used throughout an applicatito

Daniel González 129 Nov 20, 2022
PHP Cache Duration

PHP Cache Duration Introduction A readable and fluent way to generate PHP cache time. Built and written by Ajimoti Ibukun Quick Samples Instead of thi

null 27 Nov 8, 2022
Twig cache extension

Twig cache extension This extension moved to the Twig organization. Check out the repository over there instead: https://github.com/twigphp/twig-cache

Alexander 394 Nov 20, 2022
This is a Symfony bundle that lets you you integrate your PSR-6 compliant cache service with the framework

PSR-6 Cache bundle This is a Symfony bundle that lets you you integrate your PSR-6 compliant cache service with the framework. It lets you cache your

null 43 Oct 7, 2021