spatie/laravel-blink
Composer 安装命令:
composer require spatie/laravel-blink
包简介
Cache that expires in the blink of an eye
关键字:
README 文档
README
This package contains a helper function (and a Facade should you prefer that) called blink that can cache values. The cache only spans the length of a single request.
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
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/laravel-blink
To enable the package, register the service provider, and optionally register the facade:
// config/app.php 'providers' => [ // ... Spatie\LaravelBlink\BlinkServiceProvider::class, ], 'aliases' => [ ... 'Blink' => Spatie\LaravelBlink\BlinkFacade::class, ],
Usage
A Blink instance can just be newed up.
$blink = new \Spatie\Blink\Blink()
You can call the following methods on it:
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 */
You can use this to avoid using static variables to cache stuff.
This piece of code
function foo() { static $result = null; if (is_null($result) { $result = ...// do some expensive stuff here } return $result; }
can be rewritten to
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
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/laravel-blink 适用场景与选型建议
spatie/laravel-blink 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.22M 次下载、GitHub Stars 达 162, 最近一次更新时间为 2017 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「laravel-blink」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/laravel-blink 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/laravel-blink 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/laravel-blink 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
Manage events on a Google Calendar
统计信息
- 总下载量: 1.22M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 163
- 点击次数: 16
- 依赖项目数: 11
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-17