simplesoftwareio/simple-cache

一个易于使用的缓存特性,用于Laravel的Eloquent模型。

1.0.0 2016-12-21 00:54 UTC

This package is not auto-updated.

Last update: 2024-09-12 04:15:44 UTC


README

Build Status Latest Stable Version Latest Unstable Version License Total Downloads

尝试我们的简单免费文件传输服务 keep.sh

配置

Composer

首先,将Simple Cache包添加到您的composer.json文件中的require部分。

"require": {
	"simplesoftwareio/simple-cache": "~1"
}

然后,运行composer update命令。

用法

您可以通过将特性添加到您选择的Eloquent模型来使用缓存特性。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SimpleSoftwareIO\Cache\Cacheable;

class User extends Model
{
    use Cacheable;
}

是的,它真的非常简单易用。设置将使用在您的Laravel应用程序中设置的默认缓存存储。此外,默认情况下,模型将被缓存30分钟。

属性

cacheLength

您可以通过修改模型上的cacheLength属性来调整默认的缓存长度。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SimpleSoftwareIO\Cache\Cacheable;

class User extends Model
{
    use Cacheable;
    protected $cacheLength = 60; //Will cache for 60 minutes
}

cacheStore

您还可以通过修改cacheStore属性来调整配置的缓存存储。缓存存储需要在应用程序的config/cache.php配置文件中设置。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SimpleSoftwareIO\Cache\Cacheable;

class User extends Model
{
    use Cacheable;
    protected $cacheStore = 'redis'; //Will use the configured `redis` store set up in your `config/cache.php` file.
}

cacheBusting

当模型运行insert/update/delete命令时,缓存破坏将自动使缓存无效。您可以通过在Eloquent模型上将cacheBusting属性设置为true来启用此功能。默认情况下此功能是禁用的。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SimpleSoftwareIO\Cache\Cacheable;

class User extends Model
{
    use Cacheable;
    protected $cacheBusting = true;

}

请注意!高插入/更新/删除流量的Eloquent模型不应使用缓存破坏功能。大量的更改将使模型频繁无效,并使缓存变得无用。如果需要最新数据,最好设置较短的缓存长度以频繁无效结果。

方法

flush()

flush方法将清除模型的缓存。

(new User)->flush()  //Cache is flushed for the `User` model.

isBusting()

isBusting将返回当前cacheBusting属性的状态。

if((new User)->isBusting()) {
    // Is cache busting
}

remember($length)

remember将设置记住Eloquent查询的分钟数。

User::remember(45)->where('id', 4')->get();

rememberForever()

rememberForever将永久记住一个查询。技术上来说是10年,但让我们假装它是永久的,好吗?

User::rememberForever()->where('id', 4')->get();

dontRemember()

最后,dontRemember将不会缓存查询结果。

User::dontRemember(0)->where('id', 4')->get();