tequilarapido/result-cache

A simple and decoupled way to store resource consuming operations results in cache.

1.0.0 2018-09-17 10:19 UTC

This package is auto-updated.

Last update: 2024-10-24 05:34:55 UTC


README

A simple and decoupled way to store resource consuming operations results in cache.

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage

Laravel Translation Sheet

Contents

Installation

You can install the package using composer

$ composer require tequilarapido/result-cache

Usage

Cache

  • Create a class that extends ResultCache
use Tequilarapido\ResultCache\ResultCache;

class BooksCache extends ResultCache {

    public function key() {
        return 'app.books.all';
    }
    
    public function data() {
        // Some heavy / resources consuming operations
        // ...
        return $books;
    }
}
  • Now you can simply call the get method to fetch cache. If the cache is invalid or not yet created, the operations will be executed.
class SomeController {
    public function books() {
          return (new BooksCache)->get();
    }
}
  • Clean the cache

The package uses the default cache driver defined in your laravel application.

You can clean the cache using the artisan cache:clear

You can also clean the cache programmatically using :

  (new BooksCache)->forget()

Application locale aware cache

Sometimes we need to cache something, but we need multiple versions depending on the application locale. For this kind of use case we need to extend the LocaleAwareResultCache, and define the locales that are available in our application.

  • Create a class that extends LocaleAwareResultCache
use Tequilarapido\ResultCache\LocaleAwareResultCache;

class BooksCache extends LocaleAwareResultCache {

    public function key() {
        return 'app.books.all';
    }
    
    public function data() {
        // Some heavy / resources consuming operations
        // We have access to $this->locale here to customize results according to locale
        // ...
        return $books;
    }
    
    public function  supportedLocales() {
        return ['en', 'fr', 'ar']
    }
}
  • Now you can simply call the get method to fetch cache. If the cache is invalid or not yet created, the operations will be executed.
class SomeController {
    public function books() {
         return (new BooksCache)->setLocale($locale)->get();
    }
}
  • Clean the cache

The package uses the default cache driver defined in your laravel application.

You can clean the cache using the artisan cache:clear

You can also clean the cache programmatically using :

  (new BooksCache)->forget()

this will clean cache for all locales.

Cache expiration

By default, cache is created for one day. You can override the protected $minutes property on your cache class to specify how much minutes you want your cache before it gets invalidated.

use Tequilarapido\ResultCache\ResultCache;

class BooksCache extends ResultCache {

    protected $minutes = 60; // One hour
    
    // ...
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

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

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.