nocs / laravel-retriever
Retrieve cached data made simple.
Installs: 1 736
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ^7.3 || ^8.0
- illuminate/support: ~7|~8|~9|~10|~11|~12
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ~9.0
README
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.