All Projects → TheDragonCode → laravel-cache

TheDragonCode / laravel-cache

Licence: MIT license
An improved helper for working with cache

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-cache

NFlags
Simple yet powerfull library to made parsing CLI arguments easy. Library also allow to print usage help "out of box".
Stars: ✭ 44 (-31.25%)
Mutual labels:  helper
langx-java
Java tools, helper, common utilities. A replacement of guava, apache-commons, hutool
Stars: ✭ 50 (-21.87%)
Mutual labels:  helper
jwt-auth
🔐 JSON Web Token Authentication for Laravel & Lumen
Stars: ✭ 525 (+720.31%)
Mutual labels:  laravel-package
LaravelPostcodes
A service wrapper around postcodes.io
Stars: ✭ 73 (+14.06%)
Mutual labels:  laravel-package
openfoodfacts-laravel
Open Food Facts API wrapper for Laravel
Stars: ✭ 117 (+82.81%)
Mutual labels:  laravel-package
laravel-api-tool-kit
Laravel api tool kit is a set of tools that will help you to build a fast and well-organized API using laravel best practices.
Stars: ✭ 107 (+67.19%)
Mutual labels:  laravel-package
blurhash
A PHP implementation of BlurHash with Laravel integration.
Stars: ✭ 46 (-28.12%)
Mutual labels:  laravel-package
flash
An easy way for Laravel flash notifications.
Stars: ✭ 14 (-78.12%)
Mutual labels:  laravel-package
GoGPUtils
Enhance productivity and avoid to reinvent the wheel every time that you start a Go project
Stars: ✭ 29 (-54.69%)
Mutual labels:  helper
Android-SharedPreferences-Helper
This Shared Preferences Helper library (Library size = ~15kb only) Simplifies usage of the default Android SharedPreferences Class. The developer can do in a few lines of code which otherwise would have required several. Simple to understand as compared to the default Shared Preferences class and easy to use. Can be used by simply adding the dep…
Stars: ✭ 15 (-76.56%)
Mutual labels:  helper
laravel-migrate-check
An artisan command to check for pending migrations with proper exit code
Stars: ✭ 53 (-17.19%)
Mutual labels:  laravel-package
trader
Laravel package for trader extension interface.
Stars: ✭ 43 (-32.81%)
Mutual labels:  laravel-package
unobserve
Mute and unmute Laravel observers at will.
Stars: ✭ 82 (+28.13%)
Mutual labels:  laravel-package
laravel-packages
Useful packages for Laravel projects
Stars: ✭ 22 (-65.62%)
Mutual labels:  laravel-package
laravel-two-factor-authentication
A two-factor authentication package for Laravel >= 8
Stars: ✭ 37 (-42.19%)
Mutual labels:  laravel-package
libimobiledevice-glue
A library with common code used by libraries and tools around the libimobiledevice project
Stars: ✭ 46 (-28.12%)
Mutual labels:  helper
laravel-jwt
A seamless JWT implementation for Laravel
Stars: ✭ 71 (+10.94%)
Mutual labels:  laravel-package
relations-widgets-for-backpack
Views/widgets for preview laravel relations in laravel backpack
Stars: ✭ 21 (-67.19%)
Mutual labels:  laravel-package
SQLServerTools
This repo is the home of various SQL-Server-Tools
Stars: ✭ 28 (-56.25%)
Mutual labels:  helper
Laravel-Youtube-API
A way to add an API to your Laravel app that converts youtube video's to mp3/mp4 and returns a download/stream link and information.
Stars: ✭ 19 (-70.31%)
Mutual labels:  laravel-package

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": "^3.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 Tests\Fixtures\Dto\DtoObject;
use Tests\Fixtures\Simple\CustomObject;

$arr1 = ['foo', 'bar'];
$arr2 = new ArrayObject(['foo', 'bar']);
$arr3 = DtoObject::make(['foo' => 'Foo', 'bar'=> 'Bar']);
$arr4 = new CustomObject();

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

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

Unpacking and processing of objects occurs as follows:

use DragonCode\Cache\Services\Cache;
use Tests\Fixtures\Dto\DtoObject;
use Tests\Fixtures\Simple\CustomObject;

['Foo', 'Bar'];
// as key: ['Foo', 'Bar']
// as tag: ['foo', 'bar']

new ArrayObject(['Foo', 'Bar']);
// as key: ['Foo', 'Bar']
// as tag: ['foo', 'bar']

DtoObject::make(['foo' => 'Foo', 'bar'=> 'Bar']);
// as key: ['Foo', 'Bar']
// as tag: ['foo', 'bar']

new CustomObject();
// as key: ['Foo']
// as tag: ['foo']

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

With Authentication

In some cases, it is necessary to bind the cache to certain users. To do this, we have added the withAuth helper.

use DragonCode\Cache\Services\Cache;
use Illuminate\Support\Facades\Auth;

// before
return Cache::make()->key(get_class(Auth::user()), Auth::id(), 'foo', 'bar');

// after
return Cache::make()->withAuth()->key('foo', 'bar');

When processing requests with a call to the withAuth method, the binding will be carried out not only by identifier, but also by reference to the model class, since a project can have several models with the possibility of authorization.

For example, App\Models\Employee, App\Models\User.

When Enabled

Basic

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->remember(static fn() => 'Some value');
// or
$cache->remember('Some value');
// Contains cached `Some value`

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

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

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

$cache->forget();
// Will remove the key from the cache.
use DragonCode\Cache\Services\Cache;
use App\Models\User;

$user = User::first();

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

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

$cache->remember(static fn() => $user);
// or
$cache->remember($user);
// Contains cached `$user`

$cache->get();
// Returns User model

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

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

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

Custom TTL

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

The cache will be written for the specified number of minutes, seconds or the DateTimeInterface instance.

It does not matter in which direction the time shift will be. During processing, the value is converted to the abs().

As Minutes
use Carbon\Carbon;
use DateTime;
use DragonCode\Cache\Services\Cache;
use DragonCode\Cache\Support\Ttl;

$cache = Cache::make()->ttl(10);

$cache = Cache::make()->ttl('10');

$cache = Cache::make()->ttl(fn () => 10);

$cache = Cache::make()->ttl(Carbon::now()->addDay());

$cache = Cache::make()->ttl(new DateTime('tomorrow'));

$cache = Cache::make()->ttl(Ttl::DAY);

$cache = Cache::make()->ttl(Ttl::WEEK);

$cache = Cache::make()->ttl(Ttl::MONTH);
As Seconds
use Carbon\Carbon;
use DateTime;
use DragonCode\Cache\Services\Cache;

$cache = Cache::make()->ttl(10, false);

$cache = Cache::make()->ttl('10', false);

$cache = Cache::make()->ttl(fn () => 10, false);

$cache = Cache::make()->ttl(Carbon::now()->addDay(), false);

$cache = Cache::make()->ttl(new DateTime('tomorrow'), false);
By Objects And Custom Strings

You can also store all TTL values in one place - in the config/cache.php file.

To do this, add a ttl block to the file and define a TTL for the objects.

After that you can use the following construction:

use DragonCode\Cache\Services\Cache;
use Tests\Fixtures\Simple\CustomObject;

$cache = Cache::make()->ttl(CustomObject::class);
$cache = Cache::make()->ttl(new CustomObject());
$cache = Cache::make()->ttl('custom_key');

// You can also specify that these values are in seconds, not minutes:
$cache = Cache::make()->ttl(CustomObject::class, false);
$cache = Cache::make()->ttl(new CustomObject(), false);
$cache = Cache::make()->ttl('custom_key', false);

If the value is not found, the default value will be taken, which you can also override in the configuration file.

With Contract

Starting with version 2.9.0, we added the ability to dynamically specify TTLs in objects. To do this, you need to implement the DragonCode\Contracts\Cache\Ttl contract into your object and add a method that returns one of the following types of variables: DateTimeInterface , Carbon\Carbon, string or integer.

This method will allow you to dynamically specify the TTL depending on the code being executed.

For example:

use DragonCode\Cache\Services\Cache;
use DragonCode\Contracts\Cache\Ttl;

class Foo implements Ttl
{
    protected $value;

    public function __construct(string $value)
    {
        $this->value = $value;
    }

    public function cacheTtl(): int
    {
        return $this->value === 'foo' ? 123 : 456;
    }
}

$cache = Cache::make()->ttl(new Foo('foo'));
// TTL is 7380 seconds

$cache = Cache::make()->ttl(new Foo('bar'));
// TTL is 27360 seconds

$cache = Cache::make()->ttl(new Foo('foo'), false);
// TTL is 123 seconds

$cache = Cache::make()->ttl(new Foo('bar'), false);
// TTL is 456 seconds

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->doesntHave();
// Returns `false`

$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`

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

License

This package's licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].