nnjeim / persist
Laravel 缓存辅助方法
1.0.1
2021-09-18 10:19 UTC
Requires
- php: >=7.4
Requires (Dev)
- orchestra/testbench: >=v4.0.0
- phpunit/phpunit: >=8.5.8
This package is auto-updated.
Last update: 2024-09-05 19:55:20 UTC
README
nnjeim Persist 辅助工具
一个 Laravel 缓存辅助方法。
安装
您可以通过 composer 安装此包
composer require nnjeim/persist
使用方法
Persist Facade
use Nnjeim\Fetch\Fetch;
use Nnjeim\Respond\Respond;
use Nnjeim\Persist\Persist;
class Country {
public function index() {
if (Persist::setCacheTag('countries')->setCacheKey('index')->hasCacheKey()) {
return Respond::toJson()
->setMessage('countries')
->setData(
Persist::setCacheTag('countries')
->setCacheKey('index')
->getCacheKey()
)
->withSuccess();
}
['response' => $response, 'status' => $status] = Fetch::setBaseUri('https://someapi.com')->get('countries');
if ($status === 200 && $response->success) {
$data = Persist::setCacheTag('countries')
->setCacheKey('index')
->rememberCacheForever($response->data);H
return Respond::toJson()
->setMessage('countries')
->setData($data)
->withSuccess();
}
return Respond::withErrors();
}
}
PersistHelper 实例化
use Nnjeim\Persist\PersistHelper;
use Nnjeim\Fetch\FetchHelper;
use Nnjeim\Respond\RespondHelper;
class Country {
private PersistHelper $persist;
private FetchHelper $fetch;
private RespondHelper $respond;
public function __construct(
PersistHelper $persist,
FetchHelper $fetch,
RespondHelper $respond
) {
$this->persist = $persist;
$this->fetch = $fetch;
$this->respond = $respond;
$this->persist->setCacheTag = 'countries';
$this->fetch->setBaseUri = 'https://someapi.com';
}
public function index() {
$this->persist->setCacheKey = 'index';
if ($this->persist->hasCacheKey()) {
return $this->respond
->toJson()
->setData($this->persist->getCacheKey())
->withSuccess();
}
['response' => $response, 'status' => $status] = $this->fetch->get('countries');
if ($status === 200 && $response->success) {
$data = $this->persist->rememberCacheForever($response->data);
return $this->respond
->toJson()
->setData($data)
->withSuccess();
}
return $this->respond->withErrors();
}
}
方法
设置缓存标签
Sets the cache tag string | array
@return $this setCacheTag(string | array $cacheTag, $suffix = null)
设置缓存键
Sets the cache key string
@return $this setCacheKey(string $cacheKey)
形成组合缓存键
sets the cache key with the result of the concatenatenation of multiple strings with an '_' seprator.
@return $this formCacheKey(array $strings) or formCacheKey($string1, $string2, ...)
缓存键存在
Asserts if a cache key exists
@return bool hasCacheKey(string $cacheKey)
获取缓存键
Returns the peristed cache key.
@return mixed getCacheKey(string $cacheKey)
记住缓存
returns the cached value for a given number of seconds
@return mixed rememberCache($value, $ttl)
永久记住缓存
returns the cached value
@return mixed rememberCacheForever($value)
忘记缓存键
Clears the cache of the given cache key.
@return void forgetCacheKey(string $cacheKey)
按标签刷新缓存
Clears all the cache keys related to a given cache tag
@return void flushCacheTag()
增加缓存键
Increments a cache key by a given value. default = 1.
@return void incrementCacheKey($amount = 1)
减少缓存键
Decrements a cache key by a given value. default = 1.
@return void decrementCacheKey($amount = 1)
变更日志
请参阅 CHANGELOG 以获取更多最近更改信息。