MemcachedMutex::acquire()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Mutex Library.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace AMF\Mutex\Adapter;
11
12
use AMF\Mutex\MutexInterface;
13
14
/**
15
 * Mutex for memcached.
16
 *
17
 * @author Amine Fattouch <[email protected]>
18
 */
19
class MemcachedMutex implements MutexInterface
20
{
21
    /**
22
     * @var \Memcached
23
     */
24
    protected $memcached;
25
26
    /**
27
     * Constructor class.
28
     *
29
     * @param \Memcached $memcached
30
     */
31
    public function __construct(\Memcached $memcached)
32
    {
33
        $this->memcached = $memcached;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function acquire($key, $ttl)
40
    {
41
        $acquired = $this->memcached->add($key, true, $ttl);
42
43
        return ($acquired === true) ? $key: null;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function release($key)
50
    {
51
        return $this->memcached->delete($key);
52
    }
53
}
54