motomedialab/runtime-store
Composer 安装命令:
composer require motomedialab/runtime-store
包简介
Store data to be shared across application for the duration of the applications runtime
README 文档
README
A simple framework-agnostic package (that plays particularly nicely with Laravel's app container, see here) that allows caching of values for the duration of a requests lifetime.
This is particularly useful in cases where scripts may be called on multiple times and depend on
third parties or query calls to return data.
Installation
You can install the package via composer:
composer require motomedialab/runtime-store
Usage
Runtime store comes with a global helper store() which uses the singleton pattern and we recommend using.
However, there are multiple ways of accessing the Runtime Store as its framework agnostic, see here.
The package utilises a similar API to Laravel's cache and session helpers, as demonstrated below.
// set a value store()->set('key', 'value') store()->put('key', 'value') store()->add('key', 'value') // check a value exists store()->has('key') // retrieve a value store()->get('key'); // will return value store()->get('key', 'default'); // has a default value // remember a value once set store()->remember('key', function () { // remember method will only execute the callback // once per runtime and return the stored value // on additional calls. return ['data' => 'value']; }); // increment / decrement numerical values store()->increment('key', 1) store()->decrement('key', 1) // forget a value after retrieving store()->pull('key') // forget a value/ multiple values store()->forget('key') store()->delete('key') store()->forget(['key1', 'key2']) // forget all values store()->clear()
Grouping
As of v1.2, you can create groups. Groups have the exact same API as above, with a few extras methods to easily create and delete groups.
// creating a group and setting your first value... store()->group('groupName')->set('key', 'value') // getting a key from a group store()->group('groupName')->get('key') // checking if a group exists store()->hasGroup('groupName') // deleting a group store()->deleteGroup('groupName')
Accessing the store
There are many ways to access the store and you can write your own implementation quickly and easily.
// in Laravel, there are multiple ways to resolve a global instance of the store... app(\Motomedialab\RuntimeStore\RuntimeStore::class)->get('value'); \Motomedialab\RuntimeStore\RuntimeStoreFacade::get('value'); app('store')->get('value'); resolve('store')->get('value'); // there is a global store() helper method, demonstrated below. // this also integrates with Laravel's application layer directly (when installed). store()->get('value');
Accessing from within a class
We've built a handy trait which you can use in your classes to instantly boot up a grouped store, scoped to that class name. In the example below, any values will be set within a group called YourClass automatically.
class YourClass { use \Motomedialab\RuntimeStore\Traits\HasRuntimeStore; public function yourMethod() { return $this->store()->remember('key', function () { return 'This value will be remembered as long as the request is active!'; }); } }
Further implementations
We've covered off pretty much everything above but if you're looking to implement the RuntimeStore in your own way, here's a couple of examples...
// procedual example $store = null; function store() { global $store; if ($store) { return $store; } return $store = new \Motomedialab\RuntimeStore\RuntimeStore; }
// OOP example class ClassWithStore { protected $store; public function store() { if ($this->store) { return $this->store; } return $this->store = new \MotoMediaLab\RuntimeStore\RuntimeStore; } }
Testing
We've carried out extensive testing to make sure everything works as expected, but feel free to try it yourself!
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Security
If you discover any security related issues, please email hello@motomedialab.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate, a super handy
package templating application.
motomedialab/runtime-store 适用场景与选型建议
motomedialab/runtime-store 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.63k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「motomedialab」 「runtime-store」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 motomedialab/runtime-store 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 motomedialab/runtime-store 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 motomedialab/runtime-store 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A helper method to generate absolute asset URL's to Vite assets
A simple audit helper that integrates directly with Laravel & FilamentPHP
Generate self-healing URLs for models
A package for enforcing GDPR compliance rules
A super lightweight request/connector pattern to make light work of API authentication and calls
An integration with BunnyCDN to upload and check on the status of streamed videos
统计信息
- 总下载量: 2.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-27