-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisStorage.php
More file actions
50 lines (39 loc) · 1.11 KB
/
RedisStorage.php
File metadata and controls
50 lines (39 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Slowmove\SimplePhpQueue\Storage\Adapters;
use Slowmove\SimplePhpQueue\Storage\StorageInterface;
use Predis\Client;
class RedisStorage implements StorageInterface
{
const DEFAULT_STORAGE_PATH = 'tcp://127.0.0.1:6379';
private Client $redisClient;
private string $storageKey;
public function __construct(
string $connectionString = self::DEFAULT_STORAGE_PATH,
string $storageKey = 'queue'
) {
$this->redisClient = new Client($connectionString);
$this->storageKey = $storageKey;
}
public function enqueue(string $data): bool
{
$res = $this->redisClient->lpush($this->storageKey, $data);
return !!$res;
}
public function dequeue(): ?string
{
return $this->redisClient->rpop($this->storageKey);
}
public function exist(string $value): bool
{
$exist = $this->redisClient->executeRaw(["LPOS", $this->storageKey, $value]);
return boolval($exist);
}
public function length(): int
{
return $this->redisClient->llen($this->storageKey);
}
public function content(): array
{
return $this->redisClient->lrange($this->storageKey, 0, -1);
}
}