spatie / laravel-blink
瞬间过期的缓存
1.7.0
2024-03-02 05:46 UTC
Requires
- php: ^7.4|^8.0
- illuminate/support: ^8.73|^9.0|^10.0|^11.0
- spatie/blink: ^1.1.3
Requires (Dev)
- orchestra/testbench: ^6.23|^8.0|^9.0
- phpunit/phpunit: ^8.5|^9.5|^10.5
README
此包包含一个名为 blink
的辅助函数(如果您更喜欢,还有一个 Facade),可以缓存值。该缓存仅持续单个请求的长度。
blink()->put('key', 'value'); blink()->get('key'); // Returns 'value' blink()->get('prefix*'); // Returns an array of values whose keys start with 'prefix' // once will only execute the given callable if the given key didn't exist yet $expensiveFunction = function() { return rand(); }); blink()->once('random', $expensiveFunction); // returns random number blink()->once('random', $expensiveFunction); // returns the same number blink()->has('key'); // Returns true blink()->has('prefix*'); // Returns true if the blink contains a key that starts with 'prefix' // Specify a default value for when the specified key does not exist blink()->get('non existing key', 'default') // Returns 'default' blink()->put('anotherKey', 'anotherValue'); // Put multiple items in one go blink()->put(['ringo' => 'drums', 'paul' => 'bass']); blink()->all(); // Returns an array with all items blink()->forget('key'); // Removes the item blink()->forget('prefix*'); // Forget all items of which the key starts with 'prefix' blink()->flush(); // Empty the entire blink blink()->flushStartingWith('somekey'); // Remove all items whose keys start with "somekey" blink()->increment('number'); // blink()->get('number') will return 1 blink()->increment('number'); // blink()->get('number') will return 2 blink()->increment('number', 3); // blink()->get('number') will return 5 // Blink implements ArrayAccess blink()['key'] = 'value'; blink()['key']; // Returns 'value' isset(blink()['key']); // Returns true unset(blink()['key']); // Equivalent to removing the value // Blink implements Countable count(blink()); // Returns 0 blink()->put('key', 'value'); count(blink()); // Returns 1
支持我们
我们在创建最佳类别的开源包上投入了大量资源。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从家乡寄来明信片,说明您正在使用我们的哪个包。您可以在我们的联系页面上找到我们的地址。我们将发布所有收到的明信片在我们的虚拟明信片墙上。
安装
您可以通过 composer 安装此包
composer require spatie/laravel-blink
要启用该包,请注册服务提供者,并可选地注册 facade
// config/app.php 'providers' => [ // ... Spatie\LaravelBlink\BlinkServiceProvider::class, ], 'aliases' => [ ... 'Blink' => Spatie\LaravelBlink\BlinkFacade::class, ],
用法
可以简单地对 Blink
实例进行实例化。
$blink = new \Spatie\Blink\Blink()
您可以在它上调用以下方法
put
/** * Put a value in the blink cache. * * @param string|array $name * @param string|int|null $value * * @return $this */ public function put($name, $value = null)
get
/** * Get a value from the blink cache. * * This function has support for the '*' wildcard. * * @param string $name * * @return null|string */ public function get(string $name)
has
/* * Determine if the blink cache has a value for the given name. * * This function has support for the '*' wildcard. */ public function has(string $name) : bool
once
/** * Only if the given key is not present in the blink cache the callable will be executed. * * The result of the callable will be stored in the given key and returned. * * @param $key * @param callable $callable * * @return mixed */
您可以使用此功能来避免使用静态变量进行缓存。
此段代码
function foo() { static $result = null; if (is_null($result) { $result = ...// do some expensive stuff here } return $result; }
可以重写为
function foo() { return blink()->once('fooCache', function() { return ... // do some expensive stuff here }); }
all
/* * Get all values in the blink cache. */ public function all() : array
allStartingWith
/** * Get all values from the blink cache which keys start with the given string. * * @param string $startingWith * * @return array */ public function allStartingWith(string $startingWith = '') : array
forget
/** * Forget a value from the blink cache. * * This function has support for the '*' wildcard. * * @param string $key * * @return $this */ public function forget(string $key)
flush
/** * Flush all values from the blink cache. * * @return $this */ public function flush()
flushStartingWith
/** * Flush all values from the blink cache which keys start with the specified value. * * @param string $startingWith * * @return $this */ public function flushStartingWith(string $startingWith)
pull
/** * Get and forget a value from the blink cache. * * This function has support for the '*' wildcard. * * @param string $name * * @return null|string */ public function pull(string $name)
increment
/** * Increment a value from the blink cache. * * @param string $name * @param int $by * * @return int|null|string */ public function increment(string $name, int $by = 1)
decrement
/** * Decrement a value from the blink cache. * * @param string $name * @param int $by * * @return int|null|string */ public function decrement(string $name, int $by = 1)
变更日志
请参阅 CHANGELOG 了解最近更改的信息。
测试
$ composer test
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全
如果您发现有关安全性的错误,请通过 security@spatie.be 发送邮件,而不是使用问题跟踪器。
致谢
许可协议
MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件。