Proyectos de Subversion LeadersLinked - Services

Rev

Rev 333 | | Comparar con el anterior | Ultima modificación | Ver Log |

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