nocs/laravel-retriever

Retrieve cached data made simple.

v1.1.8 2025-04-02 13:38 UTC

This package is auto-updated.

Last update: 2025-05-02 14:00:41 UTC


README

Latest Version on Packagist Total Downloads

Retrieve cached data made simple

Installation

Install with composer

composer require nocs/laravel-retriever

Usage

Place your cache files under app/Retrievers (primary) or app/Cache (secondary).

Use the App\Retrievers or App\Cache namespace for your classes.

Use, as usual, studlied strings for your classnames. When retrieving use a snaked version in your key.

Define public non-static methods which return the cached values.

Basic usage

Example: app/Cache/Colors.php

<?php

namespace Nocs\Retriever\Tests\Cache;

class Colors {

    public function red()
    {
        return 'red';
    }

}

Retrieve the value with:

<?php

$value = retriever()->get('colors.red');

Using one method

When using only one method, you can use the get method. app/Cache/Colors.php

<?php

namespace Nocs\Retriever\Tests\Cache;

class Colors {

    public function get()
    {
        return 'red';
    }

}

And retrieve the value with:

<?php

$value = retriever()->get('colors');

Using namespaces

When creating vendors you can use namespaces.

Give your cache files the correct namespace, for example MyVendor\Cache.

Place your cache class files in location src/Retrievers or src/Cache.

Add the location in your ServiceProvider class in the boot section.

<?php

namespace Vendor\Package\Providers;

use Illuminate\Support\ServiceProvider;
use Nocs\Retriever\Support\Facades\Retriever;

class PackageServiceProvider extends ServiceProvider
{

    public function boot(Router $router)
    {

        Retriever::loadRetrieversFrom(__dir__.'/../Cache', 'package');

    }
}

Retrieve the value with the namespace prefixed (snaked) to the key:

<?php

$value = retriever()->get('my_vendor::colors');

Arguments

A second argument to the get call can be provided as an array of arguments that will be passed to the cache closure.

Short

Instead of using

<?php

$value = retriever()->get('key');
$value = retriever()->get('key', 'default');

you can use

<?php

$value = retriever('key', 'default');

Testing

To test, run

composer test

Security

If you discover any security related issues, please email the author instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.