1+ <?php
2+
3+ namespace Micro \Plugin \Redis \Redis \Decorator ;
4+
5+
6+ use Micro \Plugin \Redis \Redis \RedisInterface ;
7+
8+ class BaseRedisDecorator implements RedisInterface
9+ {
10+ /**
11+ * @param RedisInterface $redis
12+ */
13+ public function __construct (private readonly \Redis $ redis )
14+ {
15+ }
16+
17+ /**
18+ * @param string $host can be a host, or the path to a unix domain socket
19+ * @param int $port optional
20+ * @param float $timeout value in seconds (optional, default is 0 meaning unlimited)
21+ * @param string|null $persistentId identity for the requested persistent connection
22+ * @param int $retryInterval retry interval in milliseconds.
23+ * @param float $readTimeout value in seconds (optional, default is 0 meaning unlimited)
24+ *
25+ * @return bool
26+ */
27+ public function connect (string $ host ,
28+ int $ port = 6379 ,
29+ float $ timeout = 0.0 ,
30+ string $ persistentId = null ,
31+ int $ retryInterval = 0 ,
32+ float $ readTimeout = 0.0 ): bool
33+ {
34+ return $ this ->redis ->connect (
35+ $ host ,
36+ $ port ,
37+ $ timeout ,
38+ $ persistentId ,
39+ $ retryInterval ,
40+ $ readTimeout ,
41+ );
42+ }
43+
44+ /**
45+ * @param string $host can be a host, or the path to a unix domain socket
46+ * @param int $port optional
47+ * @param float $timeout value in seconds (optional, default is 0 meaning unlimited)
48+ * @param string|null $persistentId identity for the requested persistent connection
49+ * @param int $retryInterval retry interval in milliseconds.
50+ * @param float $readTimeout value in seconds (optional, default is 0 meaning unlimited)
51+ *
52+ * @return bool
53+ */
54+ public function pconnect (string $ host ,
55+ int $ port = 6379 ,
56+ float $ timeout = 0.0 ,
57+ string $ persistentId = null ,
58+ int $ retryInterval = 0 ,
59+ float $ readTimeout = 0.0 ): bool
60+ {
61+ return $ this ->redis ->pconnect (
62+ $ host ,
63+ $ port ,
64+ $ timeout ,
65+ $ persistentId ,
66+ $ retryInterval ,
67+ $ readTimeout
68+ );
69+ }
70+
71+ /**
72+ * {@inheritDoc}
73+ */
74+ public function subscribe (array $ channels , array |string |callable $ callback ): mixed
75+ {
76+ return $ this ->redis ->subscribe ($ channels , $ callback );
77+ }
78+
79+ /**
80+ * {@inheritDoc}
81+ */
82+ public function unsubscribe (array $ channels ): mixed
83+ {
84+ return $ this ->prepareReturnResult ($ this ->redis ->rawCommand ('UNSUBSCRIBE ' , '' ));
85+ //return $this->redis->unsubscribe($channels);
86+ }
87+
88+ /**
89+ * {@inheritDoc}
90+ */
91+ public function publish (string $ channel , string $ message ): int |RedisInterface
92+ {
93+ return $ this ->prepareReturnResult ($ this ->redis ->publish ($ channel , $ message ));
94+ }
95+
96+ /**
97+ * {@inheritDoc}
98+ */
99+ public function pubsub (string $ keyword , array |string $ argument ): array |int |RedisInterface
100+ {
101+ return $ this ->prepareReturnResult ($ this ->redis ->pubsub ($ keyword , $ argument ));
102+ }
103+
104+ /**
105+ * {@inheritDoc}
106+ */
107+ public function setex (string $ key , int $ expire , mixed $ value ): bool |RedisInterface
108+ {
109+ return $ this ->prepareReturnResult ($ this ->redis ->setex ($ key , $ expire , $ value ));
110+ }
111+
112+ /**
113+ * {@inheritDoc}
114+ */
115+ public function setOption (int $ option , mixed $ value ): bool
116+ {
117+ return $ this ->redis ->setOption ($ option , $ value );
118+ }
119+
120+ /**
121+ * {@inheritDoc}
122+ */
123+ public function set (string $ key , mixed $ data , int $ timeout = null ): bool |self
124+ {
125+ return $ this ->prepareReturnResult ($ this ->redis ->set ($ key , $ data , $ timeout ));
126+ }
127+
128+ /**
129+ * {@inheritDoc}
130+ */
131+ public function del ($ key1 , ...$ otherKeys ): int |self
132+ {
133+ return $ this ->prepareReturnResult ($ this ->redis ->del ($ key1 , ...$ otherKeys ));
134+ }
135+
136+ /**
137+ * @param mixed $result
138+ * @return mixed
139+ */
140+ protected function prepareReturnResult (mixed $ result ): mixed
141+ {
142+ if (!$ result ) {
143+ return $ result ;
144+ }
145+
146+ if ($ result instanceof \Redis) {
147+ return $ this ;
148+ }
149+
150+ return $ result ;
151+ }
152+
153+ /**
154+ * @param string $name
155+ * @param array $arguments
156+ *
157+ * @return mixed
158+ */
159+ public function __call (string $ name , array $ arguments ): mixed
160+ {
161+ if (!method_exists ($ this ->redis , $ name )) {
162+ throw new \BadMethodCallException (sprintf (
163+ 'Method "%s" is not exists in the "%s" ' ,
164+ $ name , get_class ($ this ->redis )
165+ ));
166+ }
167+
168+ return $ this ->prepareReturnResult ($ this ->redis ->{$ name }(...$ arguments ));
169+ }
170+ }
0 commit comments