swayok / alternative-laravel-cache
替代 Laravel 的 Redis 和文件缓存存储,正确实现标签概念。由 http://www.php-cache.com/ 提供的缓存池实现驱动。
Requires
- php: >=7.2.5
- cache/adapter-common: ^1.3.0
- cache/hierarchical-cache: ^1.2.0
- laravel/framework: >=6.0
Requires (Dev)
- cache/memcached-adapter: ^1.2.0
- cache/predis-adapter: ^1.2.0
- cache/redis-adapter: ^1.2.0
- phpunit/phpunit: ^10.5
- swayok/cache-filesystem-adapter: ^1.3.2
Suggests
- cache/memcached-adapter: Required to use Memcache-based cache
- cache/predis-adapter: Required to use Redis-based cache through predis/predis package
- cache/redis-adapter: Required to use Redis-based cache through php-redis extension (recommended - faster and more stable then predis)
- swayok/cache-filesystem-adapter: Required to use file-based cache
This package is auto-updated.
Last update: 2024-09-22 18:18:02 UTC
README
这是 Laravel Redis 和文件缓存存储的全功能替代品。所有存储支持适当的标签。由 http://www.php-cache.com/ 提供的缓存池 + 我基于 http://www.php-cache.com/ 提供的 FilesystemCachePool
代码添加了 HierarchialFilesystemCachePool
。此库中的所有类都是 Laravel 缓存系统和 http://www.php-cache.com/ 缓存池之间的代理。我与 php-cache.com 和那里的任何缓存池都没有任何关系。因此,我无法修复或更改缓存池的工作方式。
什么是适当的标签?
例如,你拥有
Cache::tags(['tag1', 'tag2'])->put('tag-test1', 'ok', 20);
Laravel 原生缓存与标签和 Redis (Laravel 5.2) 的交互方式
Cache::tags(['tag1', 'tag2'])->get('tag-test1'); //< 'ok'
Cache::tags(['tag2', 'tag1'])->get('tag-test1'); //< null
Cache::tags(['tag1'])->get('tag-test1'); //< null
Cache::tags(['tag2'])->get('tag-test1'); //< null
Cache::get('tag-test1'); //< null
Cache::forget('tag-test1'); //< won't delete anything
Cache::tags(['tag1', 'tag2'])->forget('tag-test1'); //< deleted
Cache::tags(['tag2', 'tag1'])->forget('tag-test1'); //< won't delete anything
Cache::tags(['tag1'])->forget('tag-test1'); //< won't delete anything
Cache::tags(['tag2'])->forget('tag-test1'); //< won't delete anything
Cache::tags(['tag1'])->flush(); //< won't delete anything
Cache::tags(['tag2'])->flush(); //< won't delete anything
Cache::tags(['tag1', 'tag2'])->flush(); //< flushed
Cache::tags(['tag2', 'tag1'])->flush(); //< won't delete anything
如果你认为这是正确的行为 - 离开,你不需要这个库。
当我尝试使用 Laravel 的标签版本时,我相当困惑。Laravel 的版本像文件夹(分层缓存)一样工作,而不是像标签一样。我试图了解 Laravel 的标签可以用于什么目的,但未找到任何信息。在几乎所有情况下都是无用的。希望 http://www.php-cache.com/ 提供了兼容的驱动程序。
如何使用此库
Cache::tags(['tag1', 'tag2'])->get('tag-test1'); //< 'ok' - use Cache::get('tag-test1') instead
Cache::tags(['tag2', 'tag1'])->get('tag-test1'); //< 'ok' - use Cache::get('tag-test1') instead
Cache::tags(['tag1'])->get('tag-test1'); //< 'ok' - use Cache::get('tag-test1') instead
Cache::tags(['tag2'])->get('tag-test1'); //< 'ok' - use Cache::get('tag-test1') instead
Cache::get('tag-test1'); //< 'ok'
Cache::forget('tag-test1'); //< deleted
Cache::tags(['tag1', 'tag2'])->forget('tag-test1'); //< deleted - use Cache::forget('tag-test1') instead
Cache::tags(['tag2', 'tag1'])->forget('tag-test1'); //< deleted - use Cache::forget('tag-test1') instead
Cache::tags(['tag1'])->forget('tag-test1'); //< deleted - use Cache::forget('tag-test1') instead
Cache::tags(['tag2'])->forget('tag-test1'); //< deleted - use Cache::forget('tag-test1') instead
Cache::tags(['tag1'])->flush(); //< deleted all cache entries with tag 'tag1'
Cache::tags(['tag2'])->flush(); //< deleted all cache entries with tag 'tag2'
Cache::tags(['tag1', 'tag2'])->flush(); //< deleted all cache entries with tag 'tag1' or 'tag2'
Cache::tags(['tag2', 'tag1'])->flush(); //< deleted all cache entries with tag 'tag2' or 'tag1'
注意,这里的标签类似于缓存条目的软分组。这意味着您不需要指定标签来访问/设置/删除特定的缓存键。缓存键是您需要知道以执行此操作的唯一信息。标签的目的是为您提供一种使用一行代码删除大量缓存条目的可能性。当您需要存储与同一组相关的许多条目,并且当某些内容发生变化时,需要一次性删除所有缓存条目时,标签非常有用。
例如
- 您在项目的许多地方缓存来自
users
表的数据库记录,并使用users
标签进行标记。 - 您缓存来自
orders
表的数据库记录,并使用users
和orders
标签进行标记。 - 某个用户更新了他的数据,这一动作使与该用户和
users
表相关的所有缓存条目无效。 - 您需要删除与
users
相关的所有缓存条目(1 和 2),您可以这样做:Cache::tags(['users'])->flush();
。
这样,将删除在 1 和 2 中创建的所有缓存条目。您不需要知道标签即可通过其键访问任何缓存条目。
如何使用它
Laravel 10+
添加到 composer.json
"require": {
"swayok/alternative-laravel-cache": "6.1.*"
}
Laravel 5.4+
添加到 composer.json
"require": {
"swayok/alternative-laravel-cache": "5.4.*"
}
Laravel 5.3
添加到 composer.json
"require": {
"swayok/alternative-laravel-cache": "5.3.*"
}
文件系统支持
添加到 composer.json
"require": {
"swayok/cache-filesystem-adapter": "^1.0.0"
}
Redis 支持
要使用 predis
,请将其添加到 composer.json
"require": {
"cache/predis-adapter": "^1.0"
}
要使用 php-redis
扩展,请将其添加到 `composer.json`
"require": {
"ext-redis": "*",
"cache/redis-adapter": "^1.0"
}
Memcached 支持(在 Windows 上不受支持!)
添加到 composer.json
"require": {
"ext-memcached": "*",
"cache/memcached-adapter": "^1.0"
}
对于 Windows,只有 memcache
扩展(不带结尾的 D),但在 Laravel 中没有这样的驱动程序。
声明 ServiceProvider
Laravel 5.6+
包自动发现将生效。
Laravel < 5.6
添加到 config/app.php
$providers = [
\AlternativeLaravelCache\Provider\AlternativeCacheStoresServiceProvider::class,
]
支持的缓存驱动程序
redis
- 带有适当标签的redis缓存,也支持分层缓存键;memcached
- 带有适当标签的memcached缓存,也支持分层缓存键;file
- 带有适当标签的简单基于文件的缓存;hierarchial_file
- 带有适当标签的分层基于文件的缓存。此驱动程序还支持使用/
而不是|
,因此您可以使用/users/:uid/followers/:fid/likes
而不是|users|:uid|followers|:fid|likes
,因为它更好地代表了文件系统中的路径。
缓存键中的管道字符 |
(分层缓存键)
对于 redis
、memcached
和 hierarchial_file
驱动程序,管道字符 |
作为层次分隔符。这意味着包含 |
的缓存键将作为层次结构工作。详情请参阅:http://www.php-cache.com/en/latest/hierarchy/
// Put key with colons (treated as usual cache key)
Cache::put('cache-key:something:something-else', 'value', now()->addHours(6));
// Put key with pipes (treated as hierarchical cache key)
Cache::put('cache-key|something|something-else', 'value', now()->addHours(6));
// Get key with colons
Cache::get('cache-key:something:something-else');
"value"
// Get key with pipes
Cache::get('cache-key|something|something-else');
"value"
// Forget call (it will both remove the cache key called 'cache-key' and whole hierarchy)
Cache::forget('cache-key');
// Get key with colons
Cache::get('cache-key:something:something-else');
"value"
// Get key with pipes
Cache::get('cache-key|something|something-else');
null
缓存键中的斜杠字符 /
对于 hierarchial_file
驱动程序,斜杠字符 /
作为层次分隔符,与管道字符 |
一样。这是为了模仿文件夹结构而添加的。
基于文件的缓存驱动程序的 permissions
配置参数(config/cache.php
)
'stores' => [
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache/data'),
'permissions' => [
'file' => [
'public' => 0644,
],
'dir' => [
'public' => 0755,
],
]
],
],
这些权限传递给 vendor/league/flysystem/src/Adapter/Local.php
并与默认权限合并。有两种类型:public
和 private
,但 AlternativeLaravelCache
中只会使用 public
权限。
注意事项
默认情况下,服务提供程序将替换Laravel的 redis
和 file
缓存存储。您可以像这样更改此行为
class MyAlternativeCacheStoresServiceProvider extends AlternativeCacheStoresServiceProvider {
static protected $redisDriverName = 'altredis';
static protected $memcacheDriverName = 'altmemcached';
static protected $fileDriverName = 'altfile';
}
当前文件缓存存储仅支持 'driver' => 'file'
。您可以通过
覆盖 AlternativeCacheStoresServiceProvider->makeFileCacheAdapter()
来扩展文件缓存驱动程序的列表。
是的,现在测试不多,可能永远不会更多。