mis3085/laravel-cache-label

此包已 弃用 并不再维护。未建议替代包。

Laravel 中缓存项分类的替代方法

1.0.4 2022-10-06 02:13 UTC

This package is auto-updated.

Last update: 2024-04-06 05:27:54 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包是 Laravel Cache 的辅助使用,基于 Cache Tag 语法开发。

Cache Tag 为不同的标签组合创建单独的命名空间来存储缓存项。名为 FOOBAR 的缓存项可以存在于不同的标签命名空间中并具有不同的值。

Cache Label 只建立标签与缓存项之间的引用关系。名为 FOOBAR 的缓存项将是整个缓存存储中唯一的项。

安装

您可以通过 composer 安装此包

composer require mis3085/laravel-cache-label

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="laravel-cache-label-config"

这是发布配置文件的内容

return [
    /**
     * Prefix key of every label cache
     */
    'prefix' => 'label:',

    /**
     * Affix key of every label cache
     */
    'affix' => ':key',

    /**
     * The default cache ttl of a label, null means forever.
     */
    'ttl' => null,

    /**
     * The repository class to replace the regular cache repository.
     */
    'repository' => \Mis3085\LaravelCacheLabel\CacheLabelRepository::class,

    /**
     * The behavior when calling flush()
     *
     * 'flush_labels' => clear all items of the labels, but still remmember their keys for further use.
     * 'reset_labels' => only unlink the relation between items and the label, but does not touch the value of items.
     * default => refer to the flush() of current cache store which will wipe out everything.
     *
     */
    'flush_behavior' => null,

    /**
     * The behavior when calling forget()
     *
     * true => forget the item and also remove the key from the labels.
     * false => only forget the item from cache store, but does not remove its key from the labels.
     */
    'forget_and_remove' => false,
];

用法

存储标记缓存项

cache()->labels(['people', 'artists'])->put('John', $john, $seconds);
cache()->labels(['people', 'authors'])->put('Anne', $anne, $seconds);

访问标记缓存项

以下语句返回相同的值。

cache()->labels(['people', 'artists'])->get('John');
cache()->labels(['artists', 'people'])->get('John');
cache()->labels('people')->get('John');
cache()->labels('artists')->get('John');
cache()->get('John');

缓存键必须在每个标签中存在,否则将返回空或默认值。

cache()->labels(['authors', 'people'])->get('John');
// null, John does not belong to authors

清除标记缓存项

清除与标签关联的项的值,但仍然记住缓存键以供进一步使用。

cache()->labels('artists')->flushLabels();

cache()->get('John');
// null, been wiped by the "artists" label
cache()->put('John', 'john-updated-1', $seconds);
cache()->get('John');
// "john-updated-1"

cache()->labels('people')->flushLabels();

cache()->get('John');
// null, been wiped by the "people" label

cache()->get('Anne');
// null, also been wiped

重置标签

让标签不再跟踪任何缓存项,但不会清除其先前关联的缓存项的值。

cache()->put('John', 'john-updated-2', $seconds);

cache()->labels('artists')->resetLabels();
// No longer track any cache items

cache()->labels('artists')->flushLabels();
cache()->get('John');
// "john-updated-2"

使用 labels() 调用 forget()

当使用 labels() 调用 forget() 时,缓存项的值将被清除,但标签不会忘记缓存项的键。可以通过修改 config('cache-label.forget_and_remove') 来更改此行为。当设置为 true 时,标签将在其值被清除后忘记缓存键。

默认情况下

cache()->labels(['people', 'artists'])->put('John', $john, $seconds);
cache()->labels('artists')->forget('John');
cache()->get('John');
// null

cache()->put('John', 'john-updated-1', $seconds);
cache()->labels('artists')->get('John');
// "john-updated-1", the "artists" label still can access "John"

将 config('cache-label.forget_and_remove') 设置为 true

cache()->labels(['people', 'artists'])->put('John', $john, $seconds);
cache()->labels('artists')->forget('John');
cache()->get('John');
// null

cache()->put('John', 'john-updated-1', $seconds);

cache()->labels('artists')->get('John');
// null, the "artists" label can no longer access "John"

cache()->labels('people')->get('John');
// "john-updated-1", the "people" label is unaffected

使用 labels() 调用 flush()

当调用 flush() 时,无论是否使用 labels(),整个缓存存储中的所有项都将被清除。可以通过修改 config('cache-label.flush_behavior') 来更改此行为。但是,这不被推荐,更好的方法是使用 flushLabels() 或 resetLabels() 来操作标记项。

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 变更日志

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件