Completed
Push — master ( 444e43...95941d )
by Peter
16:37 queued 14:30
created

Memcache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2014, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver;
11
12
class Memcache extends BaseDriver
13
{
14
    /**
15
     * @var \Memcache
16
     */
17
    protected $memcache;
18
19
    /**
20
     * @var string
21
     */
22
    protected $prefix;
23
24
    /**
25
     * @param \Memcache $memcache
26
     * @param string $prefix
27
     */
28
    public function __construct(\Memcache $memcache, $prefix)
29
    {
30
        $this->memcache = $memcache;
31
        $this->prefix = $prefix;
32
    }
33
34
    /**
35
     * @param string $key
36
     *
37
     * @return \DateTime|null
38
     */
39
    public function get($key)
40
    {
41
        $key = $this->prefix.$key;
42
        if ($time = $this->memcache->get($key)) {
43
            return (new \DateTime())->setTimestamp($time);
44
        }
45
46
        return null;
47
    }
48
49
    /**
50
     * @param string $key
51
     * @param \DateTime $time
52
     *
53
     * @return bool
54
     */
55
    public function set($key, \DateTime $time)
56
    {
57
        $key = $this->prefix.$key;
58
        if (!($old_time = $this->memcache->get($key)) || $old_time < $time->getTimestamp()) {
59
            return $this->memcache->set($key, $time->getTimestamp());
60
        }
61
62
        return true;
63
    }
64
65
    /**
66
     * @param string $key
67
     *
68
     * @return bool
69
     */
70
    public function remove($key)
71
    {
72
        return $this->memcache->delete($this->prefix.$key);
73
    }
74
}
75