定制 alex433/laravel-eloquent-cache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alex433/laravel-eloquent-cache

最新稳定版本:v0.9.2

Composer 安装命令:

composer require alex433/laravel-eloquent-cache

包简介

Laravel's Eloquent models caching

README 文档

README

Total Downloads Latest Stable Version Latest Unstable Version License

Laravel's Eloquent models caching

Installation

Install via composer :

composer require alex433/laravel-eloquent-cache

How it works

When Eloquent fetches models by primary key, the SQL query result are cached. Subsequently, when eloquent fetches a model by primary key, the cached result will be used. The cache entry will be flushed when you create, update, or delete a model instance.

Usage

Use the Cachable trait in the models you want to cache.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Alex433\LaravelEloquentCache\Cachable;

class Post extends Model
{
    use Cachable;
}

In next cases cache queries will be executed instead SQL queries. Also it do the trick for "belongs To" relations.

Post::find($id); // findOrFail(), findOrNew()
Post::where('id', $id)->first(); // firstOrFail(), firstOrNew(), firstOrCreate(), firstOr()
Post::whereId($id)->first();
Post::where('id', $id)->get();

You can optionally define the following properties to change default trait behavior.

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

use Alex433\LaravelEloquentCache\Cachable;

class User extends Authenticatable
{
    use Notifiable,
        Cachable;

    /**
     * Cache TTL in seconds. Defaults indefinitely
     *
     * @var int $cacheTtl
     */
    public $cacheTtl = 3600;

    /**
     * Cache store name. Defaults default cache connection
     *
     * @var string $cacheStore
     */
    public $cacheStore = 'redis';

    /**
     * Cache tags. Defaults no tags
     *
     * @var array $cacheTags
     */
    public $cacheTags = ['users'];
}

To invalidate the cache entry for a model instance, use forget method.

User::find($id)->forget();

// or

User::find($id)->forget()->refresh();

When cache tags is used, you can flush the cache for a model, use the flushCache method.

User::flushCache();

// or

User::find($id)->flushCache();

统计信息

  • 总下载量: 2.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-01