leafs/redis

为您的PHP应用程序提供的简单redis助手。

v0.1.1 2023-09-03 22:09 UTC

This package is auto-updated.

Last update: 2024-09-04 00:18:49 UTC


README




Leaf Redis

Latest Stable Version Total Downloads License

Leaf Redis提供了使用PHP与Redis交互的简单接口。它还附带了一组CLI命令,用于Leaf框架。

安装

要开始使用,请确保您的机器上已安装redis。

您可以使用Leaf CLI快速简单地安装Leaf Redis

leaf install redis

或者使用composer

composer require leafs/redis

入门指南

要开始使用Leaf Redis,您只需要调用init方法并传入您需要的任何配置。

Leaf\Redis::init();

这将初始化一个新的redis连接,从那里,您可以调用您需要的任何函数。

Aloe CLI

尽管Leaf Redis可以在Leaf环境外使用,但它在Leaf框架方面提供了更多的支持。Leaf Redis附带了对Aloe CLI的原生支持,后者用于Leaf MVC和Leaf API。要开始使用,请转到Leaf API/Leaf MVC应用的根目录中的leaf文件或注册Aloe CLI的任何位置,并注册一个新命令。

$console->register(\Leaf\Redis::commands());

从那里,您应该可以访问Leaf Redis提供的大量新命令。可用的命令包括

redis
  redis:install  Create leaf redis config and .env variables
  redis:server   Start redis server

配置

如上所述,init方法接受一个数组作为配置。以下是init的默认配置。

/*
|--------------------------------------------------------------------------
| Redis host
|--------------------------------------------------------------------------
|
| Set the host for redis connection
|
*/
'host' => '127.0.0.1',

/*
|--------------------------------------------------------------------------
| Redis host port
|--------------------------------------------------------------------------
|
| Set the port for redis host
|
*/
'port' => 6379,

/*
|--------------------------------------------------------------------------
| Redis auth
|--------------------------------------------------------------------------
|
| Set the password for redis connection
|
*/
'password' => null,

/*
|--------------------------------------------------------------------------
| Redis session handler
|--------------------------------------------------------------------------
|
| Set redis as session save handler
|
*/
'session' => false,

/*
|--------------------------------------------------------------------------
| Redis connection timeout
|--------------------------------------------------------------------------
|
| Value in seconds (optional, default is 0.0 meaning unlimited)
|
*/
'connection.timeout' => 0.0,

/*
|--------------------------------------------------------------------------
| Redis connection reserved
|--------------------------------------------------------------------------
|
| should be null if $retryInterval is specified
|
*/
'connection.reserved' => null,

/*
|--------------------------------------------------------------------------
| Redis session handler
|--------------------------------------------------------------------------
|
| Connection retry interval in milliseconds.
|
*/
'connection.retryInterval' => 0,

/*
|--------------------------------------------------------------------------
| Redis connection read timeout
|--------------------------------------------------------------------------
|
| Value in seconds (optional, default is 0 meaning unlimited
|
*/
'connection.readTimeout' => 0.0,

/*
|--------------------------------------------------------------------------
| Redis session save_path
|--------------------------------------------------------------------------
|
| Save path for redis session. Leave null to automatically
| generate the session save path. You can also use multiple save urls
| by passing in an array.
|
*/
'session.savePath' => null,

/*
|--------------------------------------------------------------------------
| Redis session save_path options
|--------------------------------------------------------------------------
|
| Options for session save path. You can pass in multiple options in
| the order of the save path above.
|
*/
'session.saveOptions' => [],
use Leaf\Redis;

Redis::init([
  // you can use multiple hosts
  'session.savePath' => ['tcp://host1:6379', 'tcp://host2:6379'],

  // the first array is for the first host, second for the second host
  'session.saveOptions' => [['weight' => 1], ['weight' => 2]],
]);

可用方法

set

这允许您设置redis条目。

Leaf\Redis::set('key', 'value');

// you can also use arrays to set multiple values at once

Leaf\Redis::set(['key' => 'value', 'key2' => 'value']);

get

这返回保存的redis条目。

$value = Leaf\Redis::get('key');

// You can also get multiple entries at once

$data = Leaf\Redis::get(['key', 'key2']);

// $data => [key => value, key2 => value]

ping

ping redis服务器

Leaf\Redis::ping();

💬 保持联系

📓 学习Leaf 3

  • Leaf具有非常易于理解的文档,其中包含有关Leaf中所有操作的信息。
  • 您还可以查看我们的YouTube频道,其中包含不同主题的视频教程。
  • 您还可以通过codelabs学习并贡献。

😇 贡献

我们很高兴有你。所有贡献都受到欢迎!要开始,请熟悉我们的贡献指南,然后您就可以准备提交第一个pull request 🚀。

要报告安全漏洞,您可以通过twitter上的@mychidarko@leafphp与我们联系。我们将协调修复并最终将解决方案提交到本项目。

🤩 赞助Leaf

您的现金贡献对我们帮助Leaf变得更好具有重要意义。您可以在open collective上赞助Leaf及其任何包,或查看贡献页面以获取贡献方式的列表。

并且,对我们所有现有的现金/代码贡献者,我们都爱你们❤️