motomedialab/runtime-store

将数据存储在应用程序运行期间以供跨应用共享

v1.3.1 2022-01-07 15:54 UTC

This package is auto-updated.

Last update: 2024-09-11 19:56:31 UTC


README

Latest Version on Packagist
StyleCI Code Quality
Build Status
Total Downloads

这是一个简单且框架无关的包(与Laravel的应用容器配合得非常好,见此处),允许在请求生命周期的整个过程中缓存值。

这在脚本可能被多次调用并依赖于第三方或查询调用以返回数据的情况下尤其有用。
数据。

安装

您可以通过composer安装此包

composer require motomedialab/runtime-store  

用法

Runtime store包含一个全局助手store(),它使用单例模式,我们建议使用它。
但是,由于它是框架无关的,有多种方式可以访问Runtime Store,见此处

该包使用类似于Laravel的cachesession助手的API,如下所示。

// 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()

分组

从v1.2开始,您可以创建组。组具有与上述完全相同的API,以及一些额外的创建和删除组的方法。

// 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')

访问存储

有多种方法可以访问存储,您可以快速轻松地编写自己的实现。

// 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');

从类中访问

我们已经构建了一个方便的特质,您可以在类中使用它来即时启动一个分组存储,限制为该类名称。在下面的示例中,任何值都将自动设置在名为YourClass的组中。

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!';
    });
  }
}

进一步实现

我们已经涵盖了上述所有内容,但如果您想以自己的方式实现RuntimeStore,这里有一些示例...

// 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;
  }
}

测试

我们已经进行了广泛的测试,以确保一切按预期工作,但您可以自由尝试!

composer test  

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

安全

如果您发现任何与安全相关的问题,请通过hello@motomedialab.com发送电子邮件,而不是使用问题跟踪器。

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。

Laravel包模板

此包是用Laravel包模板生成的,这是一个超级方便的
包模板应用程序。