Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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