Completed
Push — master ( 2a4746...7c3af8 )
by Peter
08:23
created

Memcached::__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 1
Metric Value
c 1
b 0
f 1
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
 * @package   AnimeDb
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2014, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver;
12
13
/**
14
 * Memcached driver
15
 *
16
 * @package AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver
17
 * @author  Peter Gribanov <[email protected]>
18
 */
19
class Memcached extends BaseDriver
20
{
21
    /**
22
     * @var \Memcached
23
     */
24
    protected $memcached;
25
26
    /**
27
     * @var string
28
     */
29
    protected $prefix;
30
31
    /**
32
     * @param \Memcached $memcached
33
     * @param string $prefix
34
     */
35
    public function __construct(\Memcached $memcached, $prefix)
36
    {
37
        $this->memcached = $memcached;
38
        $this->prefix = $prefix;
39
    }
40
    /**
41
     * @param string $key
42
     *
43
     * @return \DateTime|null
44
     */
45
    public function get($key)
46
    {
47
        $key = $this->prefix.$key;
48
        if ($time = $this->memcached->get($key)) {
49
            return (new \DateTime())->setTimestamp($time);
50
        }
51
52
        return null;
53
    }
54
55
    /**
56
     * @param string $key
57
     * @param \DateTime $time
58
     *
59
     * @return boolean
60
     */
61
    public function set($key, \DateTime $time)
62
    {
63
        $key = $this->prefix.$key;
64
        if (!($old_time = $this->memcached->get($key)) || $old_time < $time->getTimestamp()) {
65
            $this->memcached->set($key, $time->getTimestamp());
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * @param string $key
73
     *
74
     * @return boolean
75
     */
76
    public function remove($key)
77
    {
78
        return $this->memcached->delete($this->prefix.$key);
79
    }
80
}
81