1: | <?php
2: | namespace Opencart\System\Library\Session;
3: | /**
4: | * Class Redis
5: | *
6: | * @package Opencart\System\Library\Session
7: | */
8: | class Redis {
9: | private object $config;
10: | private \Redis $redis;
11: | public string $prefix;
12: |
13: | /**
14: | * Constructor
15: | *
16: | * @param \Opencart\System\Engine\Registry $registry
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.'; // session prefix to identify session keys
25: | } catch (\RedisException $e) {
26: | }
27: | }
28: |
29: | /**
30: | * Read
31: | *
32: | * @param string $session_id
33: | *
34: | * @return array<mixed>
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: | * Write
48: | *
49: | * @param string $session_id
50: | * @param array<mixed> $data
51: | *
52: | * @return bool
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: | * Destroy
64: | *
65: | * @param string $session_id
66: | *
67: | * @return bool
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: | * GC
77: | *
78: | * @return bool
79: | */
80: | public function gc(): bool {
81: | // Redis will take care of Garbage Collection itself.
82: |
83: | return true;
84: | }
85: | }
86: |

OpenCart API API documentation generated by ApiGen dev-master