项目作者: laravie

项目描述 :
🗄️ Runtime Cache for Laravel Eloquent
高级语言: PHP
项目地址: git://github.com/laravie/cabinet.git
创建时间: 2017-11-01T10:15:31Z
项目社区:https://github.com/laravie/cabinet

开源协议:MIT License

下载


Runtime Cache for Laravel Eloquent

Cabinet allows you to attach runtime or persistent caching to any Laravel Eloquent instance.

tests
Latest Stable Version
Total Downloads
Latest Unstable Version
License

Installation

To install through composer, simply put the following in your composer.json file:

  1. {
  2. "require": {
  3. "laravie/cabinet": "^3.0"
  4. }
  5. }

And then run composer install or composer update from the terminal.

Quick Installation

Above installation can also be simplify by using the following command:

  1. composer require "laravie/cabinet=^3.0"

Usages

Setup Cabinet on a Model

You first need to add Laravie\Cabinet\Cabinet on an Eloquent Model such as:

  1. <?php
  2. namespace App;
  3. use Illuminate\Foundation\Auth\User as Authenticatable;
  4. use Laravie\Cabinet\Cabinet;
  5. class User extends Authenticatable
  6. {
  7. use Cabinet;
  8. }

Allow persistent caching

To add persistent caching on the Eloquent, you have to attach a cache storage that support tags (Cache tags are not supported when using the file or database cache driver).

  1. <?php
  2. namespace App;
  3. use Illuminate\Foundation\Auth\User as Authenticatable;
  4. use Laravie\Cabinet\Cabinet;
  5. class User extends Authenticatable
  6. {
  7. use Cabinet;
  8. /**
  9. * Configure cabinet for the eloquent model.
  10. *
  11. * @param \Laravie\Cabinet\Repository $cabinet
  12. * @return void
  13. */
  14. protected function onCabinet($cabinet)
  15. {
  16. $cabinet->setStorage(resolve('cache.store'));
  17. }
  18. }

Storing data

Runtime

  1. Laravie\Cabinet\Repository::share(string $key, callable $callback);

The method allows a value to be register for $key using a closure/callable $callback.

  1. $user->cabinet()->share('birthday', static function ($user) {
  2. return now()->diffInDays($user->birthdate);
  3. });

Persistent with TTL

  1. Laravie\Cabinet\Repository::share(string $key, callable $callback, $ttl = null);

By adding the 3rd parameter $ttl (in seconds), Cabinet will attempt to store the data in cache for $ttl seconds.

  1. $user->cabinet()->share('birthday', static function ($user) {
  2. return now()->diffInDays($user->birthdate);
  3. }, 60);

Forever

  1. Laravie\Cabinet\Repository::forever(string $key, callable $callback);

You can either use forever as the 3rd parameter using share or use forever to cache the value indefinitely.

  1. $user->cabinet()->share('birthday', static function ($user) {
  2. return now()->diffInDays($user->birthdate);
  3. }, 'forever');
  4. // or
  5. $user->cabinet->forever('birthday', static function ($user) {
  6. return now()->diffInDays($user->birthdate);
  7. })

Retrieving the data

  1. Laravie\Cabinet\Repository::get(string $key);

Retrieving the data using get method.

  1. $user->cabinet()->get('birthday');
  2. // or
  3. $user->cabinet('birthday');

Forgetting the data

  1. Laravie\Cabinet\Repository::forget(string $key);

Forget any data by the given $key.

  1. $user->cabinet()->forget('birthday');

Flushing all data

  1. Laravie\Cabinet\Repository::flush();

Flushing all data for an Eloquent model.

  1. $user->cabinet()->flush();