Proyectos de Subversion LeadersLinked - Services

Rev

Rev 333 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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