@@ -45,39 +45,57 @@ public function __construct($options) {
45
45
}
46
46
}
47
47
48
+ private function key ($ namespace , $ key = '' ) {
49
+ return $ this ->cachePrefix . ($ namespace ? ". $ namespace " : '' ) . $ key ;
50
+ }
51
+
48
52
public function get ($ namespace , $ key ) {
49
- $ data = $ this ->redis ->get ($ this ->options [ ' prefix ' ] . $ key );
53
+ $ data = $ this ->redis ->get ($ this ->key ( $ namespace , $ key) );
50
54
51
55
return json_decode ($ data , true );
52
56
}
53
57
54
58
public function set ($ namespace , $ key , $ value , $ expire = null ) {
55
- $ expire = $ expire ?? $ this ->options [ ' expire ' ] ;
56
- $ prefix = $ this ->options [ ' prefix ' ] ;
57
- $ status = $ this ->redis ->set ($ prefix . $ key , json_encode ($ value ));
59
+ $ expire = $ expire ?? $ this ->expire ;
60
+ $ _key = $ this ->key ( $ namespace , $ key ) ;
61
+ $ status = $ this ->redis ->set ($ _key , json_encode ($ value ));
58
62
59
- if ($ status ) {
60
- $ this ->redis ->expire ($ prefix . $ key , $ expire );
63
+ if ($ status && $ expire ) {
64
+ $ this ->redis ->expire ($ _key , $ expire );
61
65
}
62
66
}
63
67
64
68
public function getMulti ($ namespace , $ keys , $ serverKey = false ) {
65
69
$ result = [];
66
70
67
71
foreach ($ keys as $ key ) {
68
- $ result [$ key ] = $ this ->get ($ namespace , $ key );
72
+ $ result [$ key ] = $ this ->get ($ this -> key ( $ namespace , $ key) );
69
73
}
70
74
71
75
return $ result ;
72
76
}
73
77
74
- public function setMulti ($ namespace , $ items , $ expire = 0 , $ serverKey = false ) {
78
+ public function setMulti ($ namespace , $ items , $ expire = null , $ serverKey = false ) {
79
+ $ expire = $ expire ?? $ this ->expire ;
80
+
75
81
foreach ($ items as $ key => $ value ) {
76
- $ this ->set ($ namespace , $ key , $ value );
82
+ $ this ->set ($ this -> key ( $ namespace , $ key) , $ value, $ expire );
77
83
}
78
84
}
79
85
80
86
public function delete ($ namespace , $ key ) {
81
- $ this ->redis ->del ($ this ->options ['prefix ' ] . $ key );
87
+ if ($ key ) {
88
+ $ keys = $ this ->key ($ namespace , $ key );
89
+ } else {
90
+ if ($ namespace ) {
91
+ $ keys = $ this ->key ($ namespace , '* ' );
92
+ } else {
93
+ $ keys = $ this ->key ('* ' );
94
+ }
95
+ }
96
+
97
+ $ this ->redis ->del ($ keys );
98
+
99
+ return true ;
82
100
}
83
101
}
0 commit comments