4
4
5
5
use Closure ;
6
6
use Exception ;
7
+ use Illuminate \Support \Str ;
7
8
use Illuminate \Contracts \Cache \Store ;
8
9
use Illuminate \Support \InteractsWithTime ;
10
+ use Illuminate \Database \PostgresConnection ;
9
11
use Illuminate \Database \ConnectionInterface ;
10
12
11
13
class DatabaseStore implements Store
@@ -78,7 +80,7 @@ public function get($key)
78
80
return ;
79
81
}
80
82
81
- return unserialize ($ cache ->value );
83
+ return $ this -> unserialize ($ cache ->value );
82
84
}
83
85
84
86
/**
@@ -93,7 +95,7 @@ public function put($key, $value, $minutes)
93
95
{
94
96
$ key = $ this ->prefix .$ key ;
95
97
96
- $ value = serialize ($ value );
98
+ $ value = $ this -> serialize ($ value );
97
99
98
100
$ expiration = $ this ->getTime () + (int ) ($ minutes * 60 );
99
101
@@ -157,7 +159,7 @@ protected function incrementOrDecrement($key, $value, Closure $callback)
157
159
158
160
$ cache = is_array ($ cache ) ? (object ) $ cache : $ cache ;
159
161
160
- $ current = unserialize ($ cache ->value );
162
+ $ current = $ this -> unserialize ($ cache ->value );
161
163
162
164
// Here we'll call this callback function that was given to the function which
163
165
// is used to either increment or decrement the function. We use a callback
@@ -172,7 +174,7 @@ protected function incrementOrDecrement($key, $value, Closure $callback)
172
174
// since database cache values are encrypted by default with secure storage
173
175
// that can't be easily read. We will return the new value after storing.
174
176
$ this ->table ()->where ('key ' , $ prefixed )->update ([
175
- 'value ' => serialize ($ new ),
177
+ 'value ' => $ this -> serialize ($ new ),
176
178
]);
177
179
178
180
return $ new ;
@@ -253,4 +255,36 @@ public function getPrefix()
253
255
{
254
256
return $ this ->prefix ;
255
257
}
258
+
259
+ /**
260
+ * Serialize the given value.
261
+ *
262
+ * @param mixed $value
263
+ * @return string
264
+ */
265
+ protected function serialize ($ value )
266
+ {
267
+ $ result = serialize ($ value );
268
+
269
+ if ($ this ->connection instanceof PostgresConnection && Str::contains ($ result , "\0" )) {
270
+ $ result = base64_encode ($ result );
271
+ }
272
+
273
+ return $ result ;
274
+ }
275
+
276
+ /**
277
+ * Unserialize the given value.
278
+ *
279
+ * @param string $value
280
+ * @return mixed
281
+ */
282
+ protected function unserialize ($ value )
283
+ {
284
+ if ($ this ->connection instanceof PostgresConnection && ! Str::contains ($ value , [': ' , '; ' ])) {
285
+ $ value = base64_decode ($ value );
286
+ }
287
+
288
+ return unserialize ($ value );
289
+ }
256
290
}
0 commit comments