bennett-treptow / laravel-cached-mutators
缓存的模型变更器
v1.2.0
2020-01-14 15:34 UTC
Requires
- php: ^7.0
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-08-30 01:11:49 UTC
README
此包为您提供的 Eloquent 模型提供了一个特性,可以自动缓存您选择的任何变更器和属性。此特性的良好用例是用于昂贵的变更器,您只想在每个请求/特定时间段内运行一次,并希望将您的变更器代码从任何手动缓存中解放出来。
此包通过挂钩 Eloquent 模型的 getAttributeValue
函数来工作,并传递值,如果指定的属性不需要根据 $cacheAttributes
缓存。
安装
composer require bennett-treptow/laravel-cached-mutators
用法
基本用法
<?php use CachedMutators\HasCachedMutators; /** * @property string $customer_id * @property \Stripe\Customer $associated_stripe_customer */ class MyModel extends Model { use HasCachedMutators; //declare your auto cached attribute keys protected $cacheAttributes = [ 'associated_stripe_customer' ]; /** * @return \Stripe\Customer */ public function getAssociatedStripeCustomer(){ //call to an external service such as Stripe //this call will be proxied through the Cache //and will only call the external service once return \Stripe\Customer::retrieve($this->customer_id); } }
高级用法
<?php use CachedMutators\HasCachedMutators; /** * @property string $customer_id * @property \Stripe\Customer $associated_stripe_customer * @property \Stripe\Source[] $associated_payment_methods */ class MyModel extends Model { use HasCachedMutators; //declare your auto cached attribute keys protected $cacheAttributes = [ 'associated_stripe_customer' => [ 'store' => 'redis', 'ttl' => null ], 'associated_payment_methods' => [ 'store' => 'redis', 'ttl' => 1000 ] ]; /** * @return \Stripe\Customer */ public function getAssociatedStripeCustomer(){ //call to an external service such as Stripe //this call will be proxied through the Cache //and will only call the external service once return \Stripe\Customer::retrieve($this->customer_id); } /** * @return \Stripe\Source[] */ public function getAssociatedPaymentMethods(){ return \Stripe\Customer::allSources($this->customer_id, [ 'object' => 'card', 'limit' => 3 ]); } }
<?php use CachedMutators\HasCachedMutators; /** * @property string $customer_id * @property \Stripe\Customer $associated_stripe_customer * @property \Stripe\Source[] $associated_payment_methods */ class MyModel extends Model { use HasCachedMutators; public static function defaultCacheStore(){ return 'redis'; } public static function defaultCacheTTL(){ return 60; } //declare your auto cached attribute keys protected $cacheAttributes = [ 'associated_stripe_customer', //will receive redis as its store and ttl of 60 'associated_payment_methods' => [ 'ttl' => 600 //will override the default specified above ] ]; /** * @return \Stripe\Customer */ public function getAssociatedStripeCustomer(){ //call to an external service such as Stripe //this call will be proxied through the Cache //and will only call the external service once return \Stripe\Customer::retrieve($this->customer_id); } /** * @return \Stripe\Source[] */ public function getAssociatedPaymentMethods(){ return \Stripe\Customer::allSources($this->customer_id, [ 'object' => 'card', 'limit' => 3 ]); } }
可以通过定义 store
和 ttl
来配置 $cacheAttributes
数组,以按属性缓存变更器。
默认情况下,store
将遵循您的应用程序的默认缓存存储,通常是 file
存储。定义 ttl
将调用缓存存储库的 remember
函数,而将 ttl
设置为 null 或不包含在数组中,将使用缓存存储库的 rememberForever
函数。您还可以在您的模型上覆盖 defaultCacheStore()
和 defaultCacheTTL()
函数,以加速选择每个属性应缓存的地点的过程。
清除缓存的变更器
需要清除您的缓存变更器以获取新副本吗?
clearCachedMutators($key = null)
<?php $myModel = new MyModel(); $stripeCustomer = $myModel->associated_stripe_customer; //do some stuff.. $myModel->clearCachedMutators(); //will clear all declared mutators in $cacheAttributes $myModel->clearCachedMutators('associated_stripe_customer'); //to just clear one key