henzeb/laravel-cache-index

标签的灵活替代品

v1.4.0 2024-03-11 10:33 UTC

This package is auto-updated.

Last update: 2024-09-11 11:39:00 UTC


README

Build Status Latest Version on Packagist Total Downloads Test Coverage License

当您需要能够跟踪键的情况时,可以使用 标签。不幸的是,并非所有驱动程序都支持标签,并且使用 redis 仅能以 hacky 方式检索键。

Laravel 缓存索引提供了一种与驱动程序无关的方式来管理此类索引。

安装

只需使用以下命令安装。

composer require henzeb/laravel-cache-index

用法

在底层,index 返回一个扩展的 Illuminate\Cache\Repository 对象,它管理您的索引。

请参阅 Laravel 文档 了解可能的方法。

键以索引名称为前缀,因此您不会意外覆盖属于该索引的任何值。

注意:当前在使用 index 时不支持标签。

Cache::index('myIndex')->add('test', 'my value');
Cache::index('myIndex')->put('put', 'my value');

Cache::driver('file')->index('myIndex')->remember('filed', 'my value in file');

Cache::index('myIndex')->get('put'); // returns 'my value'
Cache::get('put'); // returns null

Cache::index('myIndex')->flush(); // only flushes keys in index

索引名称

索引名称可以是普通字符串,也可以是数组。当您有一个基于变量的索引时,这很有用。

Cache::index(['myIndex', 'Read', 'Write']); // uses index name `myIndex.Read.Write`
Cache::index(['myIndex', ActionEnum::Read]); // uses index name `myIndex.Read`
Cache::index(['myIndex', new StdClass()]); // uses index name `myIndex.StdClass`
Cache::index(['myIndex', new StringableClass('stringed')]); // uses index name `myIndex.stringed`

检索键列表非常简单。

Cache::index('myIndex')->keys(); // returns ['test', 'put']
Cache::driver('file')->index('myIndex')->keys(); // returns ['filed']

计数

使用此方法,您可以知道索引中有多少键。

Cache::index('myIndex'); // using default driver
Cache::driver('file')->index('myIndex'); // using file driver

复制 & 移动

复制

复制缓存的项,并将键添加到新索引中。

如果成功,则返回 true,否则返回 false。

Cache::index('myIndex')->copy('myKey', 'targetIndex');
Cache::index('myIndex')->copy('myKey', 'targetIndex', 10);

移动

Copy 相同,但会移除其副本。

如果成功,则返回 true,否则返回 false。

Cache::index('myIndex')->move('myKey', 'targetIndex');
Cache::index('myIndex')->move('myKey', 'targetIndex', 10);

注意:请注意,复制和移动都不会对 TTL 执行任何操作。如果提供了新的 TTL,则会创建一个具有新 TTL 的新缓存的副本。

pop

与数组一样,获取并移除最后一个索引键并返回与该索引关联的值。

shift

与数组一样,获取并移除第一个索引键并返回与该索引关联的值。

random

返回一个随机值。

randomKey

返回一个随机键。

pullRandom

返回一个随机值并将其从缓存中移除。

syncTtl

允许您同步索引键之间的 ttl。

Cache::index('myIndex')->syncTtl(10);
Cache::index('myIndex')->syncTtl(now()->addSeconds(10));

测试此包

composer test

变更日志

请参阅 变更日志 了解最近更改的更多信息。

贡献

请参阅 贡献 了解详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件 henzeberkheij@gmail.com 而不是使用问题跟踪器。

鸣谢

许可证

GNU AGPLv。请参阅 许可证文件 了解更多信息。