Completed
Push — master ( f632e2...f55f1c )
by Peter
03:05
created

Memcache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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