Rememberable

Codacy Badge StyleCI License Total Downloads Latest Stable Version

Model caching for Laravel

This package can work independently of the Enso ecosystem.

For live examples and demos, you may visit laravel-enso.com

Installation

Comes pre-installed in Enso.

To install outside of Enso: composer require laravel-enso/rememberable

Features

  • comes with a trait that provides helper methods for quick and easy caching usage (setting and retrieving)
  • the cache lifetime may be set per-model or per-project
  • uses the Laravel cache() helper method so it is transparent to the cache mechanism/implementation

Usage

  1. Use the Rememberable trait in the CachedModel that you want to track

  2. You MUST set the caching duration by either:

    • adding a protected $cacheLifetime = 123; property in your CachedModel
    • setting the enso.config.cacheLifetime configuration value as desired
  3. In the RemoteModel where you have a belongsTo relationship to the CachedModel, you will need to define a method in the RemoteModel as below:

    public function getCachedModel()
    {
        return CachedModel::cacheGet($this->cached_model_id)
    }
    

    You can even call nested relations like this:

    $remoteModel->getCachedModel()
        ->getAnotherCachedModel()
        ->chainOtherRelationsOrMethods;
    

You may use the global enso.config.cacheLifetime configuration together with local/per-model cache lifetime values.

If given, the trait favors the per-model cache lifetime value over the global configuration value, thus you can override the global setting as required.

Contributions

are welcome. Pull requests are great, but issues are good too.

License

This package is released under the MIT license.