1: | <?php
|
2: | namespace Opencart\System\Library\Session;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Redis {
|
9: | private object $config;
|
10: | private \Redis $redis;
|
11: | public string $prefix;
|
12: |
|
13: | |
14: | |
15: | |
16: | |
17: |
|
18: | public function __construct(\Opencart\System\Engine\Registry $registry) {
|
19: | $this->config = $registry->get('config');
|
20: |
|
21: | try {
|
22: | $this->redis = new \Redis();
|
23: | $this->redis->pconnect(CACHE_HOSTNAME, CACHE_PORT);
|
24: | $this->prefix = CACHE_PREFIX . '.session.';
|
25: | } catch (\RedisException $e) {
|
26: | }
|
27: | }
|
28: |
|
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: |
|
36: | public function read(string $session_id): array {
|
37: | $data = $this->redis->get($this->prefix . $session_id);
|
38: |
|
39: | if (!$data) {
|
40: | return [];
|
41: | } else {
|
42: | return json_decode($data, true);
|
43: | }
|
44: | }
|
45: |
|
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: |
|
54: | public function write(string $session_id, array $data): bool {
|
55: | if ($session_id) {
|
56: | $this->redis->set($this->prefix . $session_id, $data ? json_encode($data) : '', $this->config->get('session_expire'));
|
57: | }
|
58: |
|
59: | return true;
|
60: | }
|
61: |
|
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: |
|
69: | public function destroy(string $session_id): bool {
|
70: | $this->redis->unlink($this->prefix . $session_id);
|
71: |
|
72: | return true;
|
73: | }
|
74: |
|
75: | |
76: | |
77: | |
78: | |
79: |
|
80: | public function gc(): bool {
|
81: |
|
82: |
|
83: | return true;
|
84: | }
|
85: | }
|
86: | |