Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16768 | Rev 16785 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16766 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Cache;
6
 
7
class CacheImpl implements CacheInterface
8
{
9
 
10
    private $memcached;
16769 efrain 11
 
16766 efrain 12
 
13
 
14
    /**
15
     *
16
     * @var int
17
     */
18
    private $ttl;
19
 
20
 
21
    /**
22
     *
23
     * @var string
24
     */
25
    private $host;
26
 
27
 
28
    /**
29
     *
30
     * @var int
31
     */
32
    private $port;
33
 
34
 
35
    /**
36
     *
37
     * @param Array $config
38
     */
16769 efrain 39
    public function __construct($config)
16766 efrain 40
    {
41
        $this->ttl    = intval($config['leaderslinked.memcache.ttl'], 10);
42
        $this->host   = strval($config['leaderslinked.memcache.host']);
43
        $this->port   = intval($config['leaderslinked.memcache.port'], 10);
44
 
45
 
46
        if(class_exists(\Memcached::class)) {
47
            $this->memcached = new \Memcached();
48
            $this->memcached->addServer($this->host, $this->port);
49
        } else  if(class_exists(\Memcache::class)) {
50
            $this->memcache = new \Memcache();
51
            $this->memcache->addserver($this->host, $this->port);
52
        }
53
 
54
    }
55
 
56
    /**
57
     *
58
     * @param string $key
59
     * @param mixed $value
60
     */
16768 efrain 61
    public function setItem($key, $value)
16766 efrain 62
    {
63
        $this->memcached->add($key, $value, $this->ttl);
64
    }
65
 
66
    /**
67
     *
68
     * @param string $key
69
     * @return boolean
70
     */
71
    public function touch($key)
72
    {
73
        return $this->memcached->touch($key, $this->ttl);
74
    }
75
 
76
 
77
    /**
78
     *
79
     * @param string $key
80
     * @return boolean
81
     */
16768 efrain 82
    public function removeItem($key)
16766 efrain 83
    {
84
        return $this->memcached->delete($key);
85
    }
86
 
87
 
16768 efrain 88
 
16766 efrain 89
    /**
90
     *
91
     * @param string $key
16768 efrain 92
     * @return boolean
93
     */
94
    public function hasItem($key)
95
    {
96
        $value = $this->memcached->get($key);
97
        if($value === \Memcached::RES_NOTFOUND) {
98
            return false;
99
        } else {
100
            return true;
101
        }
102
    }
103
 
104
 
105
    /**
106
     *
107
     * @param string $key
16766 efrain 108
     * @return mixed
109
     */
16768 efrain 110
    public function getItem($key)
16766 efrain 111
    {
112
        $value = $this->memcached->get($key);
113
        if($value === \Memcached::RES_NOTFOUND) {
114
            return false;
115
        } else {
116
            return $value;
117
        }
118
    }
119
 
120
}
121
 
122
 
123