Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Cache;
4
 
5
use Illuminate\Contracts\Cache\LockProvider;
6
use Illuminate\Contracts\Redis\Factory as Redis;
7
use Illuminate\Redis\Connections\PhpRedisConnection;
8
 
9
class RedisStore extends TaggableStore implements LockProvider
10
{
11
    /**
12
     * The Redis factory implementation.
13
     *
14
     * @var \Illuminate\Contracts\Redis\Factory
15
     */
16
    protected $redis;
17
 
18
    /**
19
     * A string that should be prepended to keys.
20
     *
21
     * @var string
22
     */
23
    protected $prefix;
24
 
25
    /**
26
     * The Redis connection instance that should be used to manage locks.
27
     *
28
     * @var string
29
     */
30
    protected $connection;
31
 
32
    /**
33
     * The name of the connection that should be used for locks.
34
     *
35
     * @var string
36
     */
37
    protected $lockConnection;
38
 
39
    /**
40
     * Create a new Redis store.
41
     *
42
     * @param  \Illuminate\Contracts\Redis\Factory  $redis
43
     * @param  string  $prefix
44
     * @param  string  $connection
45
     * @return void
46
     */
47
    public function __construct(Redis $redis, $prefix = '', $connection = 'default')
48
    {
49
        $this->redis = $redis;
50
        $this->setPrefix($prefix);
51
        $this->setConnection($connection);
52
    }
53
 
54
    /**
55
     * Retrieve an item from the cache by key.
56
     *
57
     * @param  string|array  $key
58
     * @return mixed
59
     */
60
    public function get($key)
61
    {
62
        $value = $this->connection()->get($this->prefix.$key);
63
 
64
        return ! is_null($value) ? $this->unserialize($value) : null;
65
    }
66
 
67
    /**
68
     * Retrieve multiple items from the cache by key.
69
     *
70
     * Items not found in the cache will have a null value.
71
     *
72
     * @param  array  $keys
73
     * @return array
74
     */
75
    public function many(array $keys)
76
    {
77
        $results = [];
78
 
79
        $values = $this->connection()->mget(array_map(function ($key) {
80
            return $this->prefix.$key;
81
        }, $keys));
82
 
83
        foreach ($values as $index => $value) {
84
            $results[$keys[$index]] = ! is_null($value) ? $this->unserialize($value) : null;
85
        }
86
 
87
        return $results;
88
    }
89
 
90
    /**
91
     * Store an item in the cache for a given number of seconds.
92
     *
93
     * @param  string  $key
94
     * @param  mixed  $value
95
     * @param  int  $seconds
96
     * @return bool
97
     */
98
    public function put($key, $value, $seconds)
99
    {
100
        return (bool) $this->connection()->setex(
101
            $this->prefix.$key, (int) max(1, $seconds), $this->serialize($value)
102
        );
103
    }
104
 
105
    /**
106
     * Store multiple items in the cache for a given number of seconds.
107
     *
108
     * @param  array  $values
109
     * @param  int  $seconds
110
     * @return bool
111
     */
112
    public function putMany(array $values, $seconds)
113
    {
114
        $this->connection()->multi();
115
 
116
        $manyResult = null;
117
 
118
        foreach ($values as $key => $value) {
119
            $result = $this->put($key, $value, $seconds);
120
 
121
            $manyResult = is_null($manyResult) ? $result : $result && $manyResult;
122
        }
123
 
124
        $this->connection()->exec();
125
 
126
        return $manyResult ?: false;
127
    }
128
 
129
    /**
130
     * Store an item in the cache if the key doesn't exist.
131
     *
132
     * @param  string  $key
133
     * @param  mixed  $value
134
     * @param  int  $seconds
135
     * @return bool
136
     */
137
    public function add($key, $value, $seconds)
138
    {
139
        $lua = "return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])";
140
 
141
        return (bool) $this->connection()->eval(
142
            $lua, 1, $this->prefix.$key, $this->serialize($value), (int) max(1, $seconds)
143
        );
144
    }
145
 
146
    /**
147
     * Increment the value of an item in the cache.
148
     *
149
     * @param  string  $key
150
     * @param  mixed  $value
151
     * @return int
152
     */
153
    public function increment($key, $value = 1)
154
    {
155
        return $this->connection()->incrby($this->prefix.$key, $value);
156
    }
157
 
158
    /**
159
     * Decrement the value of an item in the cache.
160
     *
161
     * @param  string  $key
162
     * @param  mixed  $value
163
     * @return int
164
     */
165
    public function decrement($key, $value = 1)
166
    {
167
        return $this->connection()->decrby($this->prefix.$key, $value);
168
    }
169
 
170
    /**
171
     * Store an item in the cache indefinitely.
172
     *
173
     * @param  string  $key
174
     * @param  mixed  $value
175
     * @return bool
176
     */
177
    public function forever($key, $value)
178
    {
179
        return (bool) $this->connection()->set($this->prefix.$key, $this->serialize($value));
180
    }
181
 
182
    /**
183
     * Get a lock instance.
184
     *
185
     * @param  string  $name
186
     * @param  int  $seconds
187
     * @param  string|null  $owner
188
     * @return \Illuminate\Contracts\Cache\Lock
189
     */
190
    public function lock($name, $seconds = 0, $owner = null)
191
    {
192
        $lockName = $this->prefix.$name;
193
 
194
        $lockConnection = $this->lockConnection();
195
 
196
        if ($lockConnection instanceof PhpRedisConnection) {
197
            return new PhpRedisLock($lockConnection, $lockName, $seconds, $owner);
198
        }
199
 
200
        return new RedisLock($lockConnection, $lockName, $seconds, $owner);
201
    }
202
 
203
    /**
204
     * Restore a lock instance using the owner identifier.
205
     *
206
     * @param  string  $name
207
     * @param  string  $owner
208
     * @return \Illuminate\Contracts\Cache\Lock
209
     */
210
    public function restoreLock($name, $owner)
211
    {
212
        return $this->lock($name, 0, $owner);
213
    }
214
 
215
    /**
216
     * Remove an item from the cache.
217
     *
218
     * @param  string  $key
219
     * @return bool
220
     */
221
    public function forget($key)
222
    {
223
        return (bool) $this->connection()->del($this->prefix.$key);
224
    }
225
 
226
    /**
227
     * Remove all items from the cache.
228
     *
229
     * @return bool
230
     */
231
    public function flush()
232
    {
233
        $this->connection()->flushdb();
234
 
235
        return true;
236
    }
237
 
238
    /**
239
     * Begin executing a new tags operation.
240
     *
241
     * @param  array|mixed  $names
242
     * @return \Illuminate\Cache\RedisTaggedCache
243
     */
244
    public function tags($names)
245
    {
246
        return new RedisTaggedCache(
247
            $this, new TagSet($this, is_array($names) ? $names : func_get_args())
248
        );
249
    }
250
 
251
    /**
252
     * Get the Redis connection instance.
253
     *
254
     * @return \Illuminate\Redis\Connections\Connection
255
     */
256
    public function connection()
257
    {
258
        return $this->redis->connection($this->connection);
259
    }
260
 
261
    /**
262
     * Get the Redis connection instance that should be used to manage locks.
263
     *
264
     * @return \Illuminate\Redis\Connections\Connection
265
     */
266
    public function lockConnection()
267
    {
268
        return $this->redis->connection($this->lockConnection ?? $this->connection);
269
    }
270
 
271
    /**
272
     * Specify the name of the connection that should be used to store data.
273
     *
274
     * @param  string  $connection
275
     * @return void
276
     */
277
    public function setConnection($connection)
278
    {
279
        $this->connection = $connection;
280
    }
281
 
282
    /**
283
     * Specify the name of the connection that should be used to manage locks.
284
     *
285
     * @param  string  $connection
286
     * @return $this
287
     */
288
    public function setLockConnection($connection)
289
    {
290
        $this->lockConnection = $connection;
291
 
292
        return $this;
293
    }
294
 
295
    /**
296
     * Get the Redis database instance.
297
     *
298
     * @return \Illuminate\Contracts\Redis\Factory
299
     */
300
    public function getRedis()
301
    {
302
        return $this->redis;
303
    }
304
 
305
    /**
306
     * Get the cache key prefix.
307
     *
308
     * @return string
309
     */
310
    public function getPrefix()
311
    {
312
        return $this->prefix;
313
    }
314
 
315
    /**
316
     * Set the cache key prefix.
317
     *
318
     * @param  string  $prefix
319
     * @return void
320
     */
321
    public function setPrefix($prefix)
322
    {
323
        $this->prefix = ! empty($prefix) ? $prefix.':' : '';
324
    }
325
 
326
    /**
327
     * Serialize the value.
328
     *
329
     * @param  mixed  $value
330
     * @return mixed
331
     */
332
    protected function serialize($value)
333
    {
334
        return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value) ? $value : serialize($value);
335
    }
336
 
337
    /**
338
     * Unserialize the value.
339
     *
340
     * @param  mixed  $value
341
     * @return mixed
342
     */
343
    protected function unserialize($value)
344
    {
345
        return is_numeric($value) ? $value : unserialize($value);
346
    }
347
}