Skip to content

Commit b2db693

Browse files
committed
feat: add redis docs
1 parent 7590907 commit b2db693

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

src/docs/database/redis.md

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,250 @@
11
# Leaf Redis
22

3+
<!-- markdownlint-disable no-inline-html -->
4+
5+
<script setup>
6+
import VideoModal from '@theme/components/shared/VideoModal.vue'
7+
</script>
8+
39
Redis is like a powerful database, but instead of storing data on a hard drive like other databases, Redis keeps everything in memory (RAM), making it much super-fast. It's often used for things that need to be accessed really fast, like caching (storing temporary data), session management, or tracking real-time data.
410

511
Leaf includes a Redis module that allows you to easily integrate Redis into your Leaf application.
12+
13+
::: details New to Redis?
14+
15+
We've included this amazing video by TechWorld with Nana to help you get started with Redis.
16+
17+
<VideoModal
18+
title="Redis intro by TechWorld with Nana"
19+
subject="Redis Crash Course - the What, Why and How to use Redis ..."
20+
description="Redis Tutorial - the What, Why and How to use Redis as a primary database."
21+
videoUrl="https://www.youtube.com/embed/OqCK95AS-YE"
22+
/>
23+
24+
:::
25+
26+
## Setting Up Leaf Redis
27+
28+
To get started with Leaf Redis, you need to have Redis installed on your machine. You can install Redis PHP extension by following the instructions [here](https://github.com/phpredis/phpredis/blob/develop/INSTALL.md).
29+
30+
After that, we can install Leaf Redis through composer or the leaf cli.
31+
32+
::: code-group
33+
34+
```bash:no-line-numbers [Leaf CLI]
35+
leaf install redis
36+
```
37+
38+
```bash:no-line-numbers [Composer]
39+
composer require leafs/redis
40+
```
41+
42+
:::
43+
44+
Once that's done, we can start using Leaf Redis in our Leaf application. Just like any other database, we need to initialize a connection to Redis before we can start using it.
45+
46+
```php
47+
redis()->connect();
48+
```
49+
50+
This will initialize a new Redis connection. From there, you can start storing and retrieving data from Redis.
51+
52+
## Usage with Leaf MVC
53+
54+
If you're using Leaf MVC, you can add on some extra features to your setup. Leaf Redis comes with a few commands that you can attach to your Aloe CLI. You can do this by heading over to the `app/console/Commands.php` file in your Leaf MVC app and adding the following line to the return array.
55+
56+
```php
57+
<?php
58+
59+
namespace App\Console;
60+
61+
class Commands
62+
{
63+
/**
64+
* Register commands
65+
*
66+
* @param $console
67+
* @return void
68+
*
69+
*/
70+
public static function register($console): void
71+
{
72+
$console->register([
73+
ExampleCommand::class,
74+
\Leaf\Redis::commands() // [!code ++]
75+
]);
76+
}
77+
}
78+
```
79+
80+
Once you've done that, you should have access to a bunch of new commands from Leaf Redis. The available commands are:
81+
82+
```bash
83+
redis
84+
redis:install Create leaf redis config and .env variables
85+
redis:server Start redis server
86+
```
87+
88+
You can then run `php leaf redis:install` to install the redis config and environment variables, and then `php leaf redis:server` to start the redis server.
89+
90+
## Configuring Leaf Redis
91+
92+
The `connect` method takes in an array for configuration. Below is the default config for `connect`.
93+
94+
```php
95+
/*
96+
|--------------------------------------------------------------------------
97+
| Redis host
98+
|--------------------------------------------------------------------------
99+
|
100+
| Set the host for redis connection
101+
|
102+
*/
103+
'host' => '127.0.0.1',
104+
105+
/*
106+
|--------------------------------------------------------------------------
107+
| Redis host port
108+
|--------------------------------------------------------------------------
109+
|
110+
| Set the port for redis host
111+
|
112+
*/
113+
'port' => 6379,
114+
115+
/*
116+
|--------------------------------------------------------------------------
117+
| Redis auth
118+
|--------------------------------------------------------------------------
119+
|
120+
| Set the password for redis connection
121+
|
122+
*/
123+
'password' => null,
124+
125+
/*
126+
|--------------------------------------------------------------------------
127+
| Redis session handler
128+
|--------------------------------------------------------------------------
129+
|
130+
| Set redis as session save handler
131+
|
132+
*/
133+
'session' => false,
134+
135+
/*
136+
|--------------------------------------------------------------------------
137+
| Redis connection timeout
138+
|--------------------------------------------------------------------------
139+
|
140+
| Value in seconds (optional, default is 0.0 meaning unlimited)
141+
|
142+
*/
143+
'connection.timeout' => 0.0,
144+
145+
/*
146+
|--------------------------------------------------------------------------
147+
| Redis connection reserved
148+
|--------------------------------------------------------------------------
149+
|
150+
| should be null if $retryInterval is specified
151+
|
152+
*/
153+
'connection.reserved' => null,
154+
155+
/*
156+
|--------------------------------------------------------------------------
157+
| Redis session handler
158+
|--------------------------------------------------------------------------
159+
|
160+
| Connection retry interval in milliseconds.
161+
|
162+
*/
163+
'connection.retryInterval' => 0,
164+
165+
/*
166+
|--------------------------------------------------------------------------
167+
| Redis connection read timeout
168+
|--------------------------------------------------------------------------
169+
|
170+
| Value in seconds (optional, default is 0 meaning unlimited
171+
|
172+
*/
173+
'connection.readTimeout' => 0.0,
174+
175+
/*
176+
|--------------------------------------------------------------------------
177+
| Redis session save_path
178+
|--------------------------------------------------------------------------
179+
|
180+
| Save path for redis session. Leave null to automatically
181+
| generate the session save path. You can also use multiple save urls
182+
| by passing in an array.
183+
|
184+
*/
185+
'session.savePath' => null,
186+
187+
/*
188+
|--------------------------------------------------------------------------
189+
| Redis session save_path options
190+
|--------------------------------------------------------------------------
191+
|
192+
| Options for session save path. You can pass in multiple options in
193+
| the order of the save path above.
194+
|
195+
*/
196+
'session.saveOptions' => [],
197+
```
198+
199+
You can pick and choose which configuration options you want to set. You can set these configurations by passing an array to the `connect` method.
200+
201+
```php
202+
redis()->connect([
203+
// you can use multiple hosts
204+
'session.savePath' => ['tcp://host1:6379', 'tcp://host2:6379'],
205+
206+
// the first array is for the first host, second for the second host
207+
'session.saveOptions' => [['weight' => 1], ['weight' => 2]],
208+
]);
209+
```
210+
211+
## Ping Pong
212+
213+
You can check if your Redis connection is working by using the `ping()` method. The `ping()` method returns a string with the message "PONG" if the connection is successful.
214+
215+
```php
216+
echo redis()->ping();
217+
```
218+
219+
## Setting values
220+
221+
You can set values in Redis using the `set()` method. The `set()` method takes in a key and a value.
222+
223+
```php
224+
redis()->set('name', 'Michael');
225+
```
226+
227+
You can also set multiple values at once by passing in an array.
228+
229+
```php
230+
redis()->set([
231+
'name' => 'Michael',
232+
'age' => 22
233+
]);
234+
```
235+
236+
## Getting values
237+
238+
You can get values from Redis using the `get()` method. The `get()` method takes in a key and returns the value.
239+
240+
```php
241+
$name = redis()->get('name');
242+
```
243+
244+
You can also get multiple values at once by passing in an array.
245+
246+
```php
247+
$values = redis()->get(['name', 'age']);
248+
249+
// $values => ['name' => 'Michael', 'age' => 22]
250+
```

0 commit comments

Comments
 (0)