Proyectos de Subversion LeadersLinked - Services

Rev

Rev 192 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Cache;
6
 
7
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
8
 
9
 
10
class CacheImpl implements CacheInterface
11
{
12
 
13
    private $memcache;
14
 
15
 
16
    /**
17
     *
18
     * @var int
19
     */
20
    private $ttl;
21
 
22
 
23
    /**
24
     *
25
     * @var string
26
     */
27
    private $host;
28
 
29
 
30
    /**
31
     *
32
     * @var int
33
     */
34
    private $port;
35
 
36
    /**
37
     *
38
     * @param Array $config
39
     */
40
    public function __construct($config)
41
    {
42
        $this->ttl    = intval($config['leaderslinked.memcache.ttl'], 10);
43
        $this->host   = strval($config['leaderslinked.memcache.host']);
44
        $this->port   = intval($config['leaderslinked.memcache.port'], 10);
45
 
46
        if(class_exists(\Memcached::class)) {
47
            $this->memcache = new \Memcached();
48
            $this->memcache->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
     * @param string $key
58
     * @param mixed $value
59
     */
60
    public function setItem($key, $value)
61
    {
62
        return $this->memcache->add($key, $value, $this->ttl);
63
    }
64
 
65
    /**
66
     *
67
     * @param string $key
68
     * @return boolean
69
     */
70
    public function touch($key)
71
    {
72
        return $this->memcache->touch($key, $this->ttl);
73
    }
74
 
75
 
76
    /**
77
     *
78
     * @param string $key
79
     * @return boolean
80
     */
81
    public function removeItem($key)
82
    {
83
        return $this->memcache->delete($key);
84
    }
85
 
86
 
87
 
88
    /**
89
     *
90
     * @param string $key
91
     * @return boolean
92
     */
93
    public function hasItem($key)
94
    {
95
 
96
        $value = $this->memcache->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
108
     * @return mixed
109
     */
110
    public function getItem($key)
111
    {
112
 
113
 
114
        $value = $this->memcache->get($key);
115
        if($value === \Memcached::RES_NOTFOUND) {
116
            return false;
117
        } else {
118
            return $value;
119
        }
120
    }
121
 
122
 
123
    /**
124
     *
125
     * @return bool
126
     */
127
    public function available()
128
    {
129
        return $this->memcache ? true : false;
130
    }
131
 
132
}
133
 
134
 
135