stevenmaguire / laravel-cache
无缝将缓存添加到Laravel服务对象
1.0.0
2015-10-22 22:46 UTC
Requires
- php: >=5.5.0
- illuminate/database: ^5.0
- illuminate/support: ^5.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: ~2.0
This package is auto-updated.
Last update: 2024-09-14 10:21:35 UTC
README
无缝将缓存添加到Laravel Eloquent服务对象。
安装
通过Composer
$ composer require stevenmaguire/laravel-cache
用法
包含基本服务
使用EloquentCache类扩展您的服务类。
class UserRegistrar extends Stevenmaguire\Laravel\Services\EloquentCache { // }
实现接口并使用特性。
class UserRegistrar implements Stevenmaguire\Laravel\Contracts\Cacheable { use \Stevenmaguire\Laravel\Services\EloquentCacheTrait; }
构造查询
使用Eloquent和请求缓存对象构建查询。
use App\User; use Stevenmaguire\Laravel\Services\EloquentCache; class UserRegistrar extends EloquentCache { public function __construct(User $user) { $this->user = $user; } public function getAllUsers() { $query = $this->user->query(); return $this->cache('all', $query); } public function getUserById($id) { $query = $this->user->where('id', $id); return $this->cache('id('.$id.')', $query, 'first'); } public function getRecent($skip = 0, $take = 100) { $query = $this->user->orderBy('created_at', 'desc') ->take($take) ->skip($skip); return $this->cache('recent('.$skip.','.$take.')', $query); } }
该cache
方法有三个参数
- 与方法的意图相关联的唯一键
- Eloquent查询的
Builder
对象 - 可选动词,如
get
、first
、list
、paginate
等;默认为get
如果与可选动词关联的方法需要参数,例如paginate
,则参数可以表示为动词和冒号后面的逗号分隔列表。如果参数期望一个字面值数组,则这些值可以表示为一个管道分隔的字符串。
/** * Paginate users with all pagination parameters */ public function getAllUsers() { $query = $this->user->query(); return $this->cache('all', $query, 'paginate:15,id|email|name,sheet,2'); // $query->paginate(15, ['id', 'email', 'name'], 'sheet', 2); }
缓存服务将自动索引应用程序使用的所有唯一键。当在实现基本缓存服务的每个服务上调用flushCache
方法时,将使用这些键。
配置缓存
对于您使用EloquentCache
实现的每个服务,您可以配置以下内容
缓存分钟数
use Stevenmaguire\Laravel\Services\EloquentCache; class UserRegistrar extends EloquentCache { protected $cacheForMinutes = 15; // }
禁用缓存
use Stevenmaguire\Laravel\Services\EloquentCache; class UserRegistrar extends EloquentCache { protected $enableCaching = false; // }
禁用日志记录
use Stevenmaguire\Laravel\Services\EloquentCache; class UserRegistrar extends EloquentCache { protected $enableLogging = false; // }
为键设置自定义缓存索引
use Stevenmaguire\Laravel\Services\EloquentCache; class UserRegistrar extends EloquentCache { protected $cacheIndexKey = 'my-service-keys-index'; // }
清除缓存
每个通过Stevenmaguire\Laravel\Services\EloquentCache
实现缓存的服务对象都可以单独清除其缓存,而不受其他消费服务的影响。
您的应用程序可以清除服务对象serviceKey
组中所有键的缓存。
$userRegistrar->flushCache();
您的应用程序只能清除与服务对象serviceKey
组中特定正则表达式模式匹配的键的缓存。
// Flush cache for all cached users with a single digit user id $userRegistrar->flushCache('^id\([0-9]{1}\)$');
绑定到IoC容器
use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { public function register() { $this->app->when('App\Handlers\Events\UserHandler') ->needs('Stevenmaguire\Laravel\Contracts\Cacheable') ->give('App\Services\UserRegistrar'); } }
在此特定示例中,UserHandler
负责在特定事件发生时清除用户服务缓存。该UserHandler
依赖于UserRegistrar
服务中的flushCache
方法。
测试
$ ./vendor/bin/phpunit
贡献
有关详细信息,请参阅CONTRIBUTING。
安全
如果您发现任何与安全相关的问题,请通过电子邮件stevenmaguire@gmail.com联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。