silassiai / laravel-table-cache
轻松缓存表记录的解决方案,该包使用laravel Cache外观。
dev-main
2023-01-02 16:19 UTC
Requires
- php: ^8
This package is auto-updated.
Last update: 2024-09-30 01:27:11 UTC
README
如果你处理大量数据,直接使用laravel缓存集合可能不是最佳实践。
代替使用(返回集合)
// for example in a cronjob that caches the users table $users = cache()->rememberForever('users', function () { return DB::table('users')->pluck('email', 'id'); }); // when you need to check if this user exists somewhere later in your application $users->where('email', 'john@do.nl')->exists();
我们可以这样做(直接在缓存中缓存模型的所有键值)
// for example in a cronjob that caches the users table User::cacheColumnKey('email')->withColumnValue('id'); // when you need to check if this user exists somewhere later in your application User::cacheColumnKey('email')->isCached('john@do.nl'); // or if you need the value User::cacheColumnKey('email')->getKeyValue('john@do.nl'); // returns the id in this case
轻松缓存表记录的解决方案,该包使用laravel Cache外观。
- 将2个表列作为键值对缓存 特性键值
安装
你可以通过composer安装此包
composer require silassiai/laravel-table-cache
基本用法
特性键值
你可以将TableCacheKeyValueTrait
添加到你的模型中,以轻松缓存整个表(两个列)的键值对。当你想在模型初始化后缓存整个表时,这非常有用。
<?php namespace App\Models; use Silassiai\LaravelTableCache\Traits\TableCacheKeyValueTrait; class BlackList { use TableCacheKeyValueTrait; }
接下来添加到你的种子文件中
public function run() { // Your seed code here... BlackList::cacheColumnKey('name')->withColumnValue('your_column_name'); }
你也可以使用默认值
public function run() { // Your seed code here... BlackList::cacheColumnKey('name')->withDefaultValue(true); }
检查值是否已缓存
BlackList::cacheColumnKey('name')->isCached('suspicious.com')
获取缓存的值
BlackList::cacheColumnKey('name')->getKeyValue('suspicious.com')
检查表列是否已经缓存
BlackList::hasTableKeyCached('name')