Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | Ir a la última revisión | | Comparar con el anterior | 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
 
8
class CacheImpl implements CacheInterface
9
{
10
 
283 www 11
    /**
333 www 12
     *
283 www 13
     * @var \LeadersLinked\Cache\CacheInterface
14
     */
15
    private static $_instance;
1 efrain 16
 
17
 
18
    /**
333 www 19
     *
283 www 20
     * @var \Memcached
21
     */
22
    private $cache;
23
 
333 www 24
 
283 www 25
    /**
333 www 26
     *
27
     * @var string
28
     */
29
    private $namespace;
30
 
31
    /**
32
     *
283 www 33
     * @var int
34
     */
35
    private $ttl;
36
 
333 www 37
 
283 www 38
    /**
1 efrain 39
     *
40
     * @param Array $config
41
     */
283 www 42
    private function __construct($config)
1 efrain 43
    {
333 www 44
 
283 www 45
 
333 www 46
        $this->ttl          = intval($config['leaderslinked.memcache.ttl'], 10);
47
        $this->namespace    = strval($config['leaderslinked.memcache.namespace']);
48
        $host               = strval($config['leaderslinked.memcache.host']);
49
        $port               = intval($config['leaderslinked.memcache.port'], 10);
192 efrain 50
 
51
 
333 www 52
        $this->cache = new \Memcached();
53
        $this->cache->addserver($host, $port);
54
 
55
 
56
 
1 efrain 57
    }
58
 
59
    /**
333 www 60
     *
283 www 61
     * @param array $config
62
     * @return \LeadersLinked\Cache\CacheInterface
1 efrain 63
     */
283 www 64
    public static function getInstance($config)
1 efrain 65
    {
283 www 66
        if(self::$_instance == null) {
67
            self::$_instance = new CacheImpl($config);
192 efrain 68
        }
283 www 69
        return self::$_instance;
1 efrain 70
    }
71
 
72
    /**
192 efrain 73
     *
1 efrain 74
     * @param string $key
283 www 75
     * @param mixed $value
333 www 76
     * @param int $ttl
1 efrain 77
     */
333 www 78
    public function setItem($key, $value, $ttl = 0)
1 efrain 79
    {
333 www 80
        $ttl = $ttl ? $ttl : $this->ttl;
81
        $key = $this->namespace ? $this->namespace . '-' . $key : $key;
192 efrain 82
 
333 www 83
        if($this->hasItem($key)) {
283 www 84
 
333 www 85
            $this->cache->replace($key, serialize($value), time() + $ttl);
192 efrain 86
        } else {
333 www 87
            $this->cache->add($key, serialize($value), time() + $ttl);
192 efrain 88
        }
1 efrain 89
    }
333 www 90
 
1 efrain 91
    /**
92
     *
93
     * @param string $key
94
     * @return boolean
95
     */
96
    public function removeItem($key)
97
    {
333 www 98
        if($this->hasItem($key)) {
99
            $key = $this->namespace ? $this->namespace . '-' . $key : $key;
100
 
283 www 101
            return $this->cache->delete($key);
192 efrain 102
        } else {
103
            return true;
104
        }
1 efrain 105
    }
106
 
107
    /**
108
     *
333 www 109
     * {@inheritDoc}
110
     * @see \LeadersLinked\Cache\CacheInterface::getAll()
1 efrain 111
     */
333 www 112
    public function getAll()
113
    {
114
        return $this->cache->fetchAll();
115
    }
116
 
117
    /**
118
     *
119
     * {@inheritDoc}
120
     * @see \LeadersLinked\Cache\CacheInterface::hasItem()
121
     */
1 efrain 122
    public function hasItem($key)
123
    {
333 www 124
        $key = $this->namespace ? $this->namespace . '-' . $key : $key;
1 efrain 125
 
333 www 126
        $keys = $this->cache->getAllKeys();
127
        if ($keys === false) {
128
            return false;
129
        }
130
        return in_array($key, $keys, true);
1 efrain 131
    }
333 www 132
 
1 efrain 133
    /**
333 www 134
     *
135
     * {@inheritDoc}
136
     * @see \LeadersLinked\Cache\CacheInterface::getItem()
1 efrain 137
     */
138
    public function getItem($key)
139
    {
333 www 140
        if($this->hasItem($key)) {
141
            $key = $this->namespace ? $this->namespace . '-' . $key : $key;
142
 
283 www 143
            $value = $this->cache->get($key);
144
            if(is_string($value)) {
145
                return unserialize($value);
146
            } else {
147
                return $value;
148
            }
1 efrain 149
        } else {
192 efrain 150
            return null;
1 efrain 151
        }
152
    }
153
 
333 www 154
}
283 www 155
 
156
 
1 efrain 157
 
158