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\Support\InteractsWithTime;
7
use Memcached;
8
use ReflectionMethod;
9
 
10
class MemcachedStore extends TaggableStore implements LockProvider
11
{
12
    use InteractsWithTime;
13
 
14
    /**
15
     * The Memcached instance.
16
     *
17
     * @var \Memcached
18
     */
19
    protected $memcached;
20
 
21
    /**
22
     * A string that should be prepended to keys.
23
     *
24
     * @var string
25
     */
26
    protected $prefix;
27
 
28
    /**
29
     * Indicates whether we are using Memcached version >= 3.0.0.
30
     *
31
     * @var bool
32
     */
33
    protected $onVersionThree;
34
 
35
    /**
36
     * Create a new Memcached store.
37
     *
38
     * @param  \Memcached  $memcached
39
     * @param  string  $prefix
40
     * @return void
41
     */
42
    public function __construct($memcached, $prefix = '')
43
    {
44
        $this->setPrefix($prefix);
45
        $this->memcached = $memcached;
46
 
47
        $this->onVersionThree = (new ReflectionMethod('Memcached', 'getMulti'))
48
                            ->getNumberOfParameters() == 2;
49
    }
50
 
51
    /**
52
     * Retrieve an item from the cache by key.
53
     *
54
     * @param  string  $key
55
     * @return mixed
56
     */
57
    public function get($key)
58
    {
59
        $value = $this->memcached->get($this->prefix.$key);
60
 
61
        if ($this->memcached->getResultCode() == 0) {
62
            return $value;
63
        }
64
    }
65
 
66
    /**
67
     * Retrieve multiple items from the cache by key.
68
     *
69
     * Items not found in the cache will have a null value.
70
     *
71
     * @param  array  $keys
72
     * @return array
73
     */
74
    public function many(array $keys)
75
    {
76
        $prefixedKeys = array_map(function ($key) {
77
            return $this->prefix.$key;
78
        }, $keys);
79
 
80
        if ($this->onVersionThree) {
81
            $values = $this->memcached->getMulti($prefixedKeys, Memcached::GET_PRESERVE_ORDER);
82
        } else {
83
            $null = null;
84
 
85
            $values = $this->memcached->getMulti($prefixedKeys, $null, Memcached::GET_PRESERVE_ORDER);
86
        }
87
 
88
        if ($this->memcached->getResultCode() != 0) {
89
            return array_fill_keys($keys, null);
90
        }
91
 
92
        return array_combine($keys, $values);
93
    }
94
 
95
    /**
96
     * Store an item in the cache for a given number of seconds.
97
     *
98
     * @param  string  $key
99
     * @param  mixed  $value
100
     * @param  int  $seconds
101
     * @return bool
102
     */
103
    public function put($key, $value, $seconds)
104
    {
105
        return $this->memcached->set(
106
            $this->prefix.$key, $value, $this->calculateExpiration($seconds)
107
        );
108
    }
109
 
110
    /**
111
     * Store multiple items in the cache for a given number of seconds.
112
     *
113
     * @param  array  $values
114
     * @param  int  $seconds
115
     * @return bool
116
     */
117
    public function putMany(array $values, $seconds)
118
    {
119
        $prefixedValues = [];
120
 
121
        foreach ($values as $key => $value) {
122
            $prefixedValues[$this->prefix.$key] = $value;
123
        }
124
 
125
        return $this->memcached->setMulti(
126
            $prefixedValues, $this->calculateExpiration($seconds)
127
        );
128
    }
129
 
130
    /**
131
     * Store an item in the cache if the key doesn't exist.
132
     *
133
     * @param  string  $key
134
     * @param  mixed  $value
135
     * @param  int  $seconds
136
     * @return bool
137
     */
138
    public function add($key, $value, $seconds)
139
    {
140
        return $this->memcached->add(
141
            $this->prefix.$key, $value, $this->calculateExpiration($seconds)
142
        );
143
    }
144
 
145
    /**
146
     * Increment the value of an item in the cache.
147
     *
148
     * @param  string  $key
149
     * @param  mixed  $value
150
     * @return int|bool
151
     */
152
    public function increment($key, $value = 1)
153
    {
154
        return $this->memcached->increment($this->prefix.$key, $value);
155
    }
156
 
157
    /**
158
     * Decrement the value of an item in the cache.
159
     *
160
     * @param  string  $key
161
     * @param  mixed  $value
162
     * @return int|bool
163
     */
164
    public function decrement($key, $value = 1)
165
    {
166
        return $this->memcached->decrement($this->prefix.$key, $value);
167
    }
168
 
169
    /**
170
     * Store an item in the cache indefinitely.
171
     *
172
     * @param  string  $key
173
     * @param  mixed  $value
174
     * @return bool
175
     */
176
    public function forever($key, $value)
177
    {
178
        return $this->put($key, $value, 0);
179
    }
180
 
181
    /**
182
     * Get a lock instance.
183
     *
184
     * @param  string  $name
185
     * @param  int  $seconds
186
     * @param  string|null  $owner
187
     * @return \Illuminate\Contracts\Cache\Lock
188
     */
189
    public function lock($name, $seconds = 0, $owner = null)
190
    {
191
        return new MemcachedLock($this->memcached, $this->prefix.$name, $seconds, $owner);
192
    }
193
 
194
    /**
195
     * Restore a lock instance using the owner identifier.
196
     *
197
     * @param  string  $name
198
     * @param  string  $owner
199
     * @return \Illuminate\Contracts\Cache\Lock
200
     */
201
    public function restoreLock($name, $owner)
202
    {
203
        return $this->lock($name, 0, $owner);
204
    }
205
 
206
    /**
207
     * Remove an item from the cache.
208
     *
209
     * @param  string  $key
210
     * @return bool
211
     */
212
    public function forget($key)
213
    {
214
        return $this->memcached->delete($this->prefix.$key);
215
    }
216
 
217
    /**
218
     * Remove all items from the cache.
219
     *
220
     * @return bool
221
     */
222
    public function flush()
223
    {
224
        return $this->memcached->flush();
225
    }
226
 
227
    /**
228
     * Get the expiration time of the key.
229
     *
230
     * @param  int  $seconds
231
     * @return int
232
     */
233
    protected function calculateExpiration($seconds)
234
    {
235
        return $this->toTimestamp($seconds);
236
    }
237
 
238
    /**
239
     * Get the UNIX timestamp for the given number of seconds.
240
     *
241
     * @param  int  $seconds
242
     * @return int
243
     */
244
    protected function toTimestamp($seconds)
245
    {
246
        return $seconds > 0 ? $this->availableAt($seconds) : 0;
247
    }
248
 
249
    /**
250
     * Get the underlying Memcached connection.
251
     *
252
     * @return \Memcached
253
     */
254
    public function getMemcached()
255
    {
256
        return $this->memcached;
257
    }
258
 
259
    /**
260
     * Get the cache key prefix.
261
     *
262
     * @return string
263
     */
264
    public function getPrefix()
265
    {
266
        return $this->prefix;
267
    }
268
 
269
    /**
270
     * Set the cache key prefix.
271
     *
272
     * @param  string  $prefix
273
     * @return void
274
     */
275
    public function setPrefix($prefix)
276
    {
277
        $this->prefix = ! empty($prefix) ? $prefix.':' : '';
278
    }
279
}