Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 17002 Rev 17018
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 58... Línea 67...
58
            self::$_instance = new CacheImpl($config);
67
            self::$_instance = new CacheImpl($config);
59
        }
68
        }
60
        return self::$_instance;
69
        return self::$_instance;
61
    }
70
    }
Línea -... Línea 71...
-
 
71
    
62
    
72
    
63
    /**
73
    /**
64
     *
74
     *
65
     * @param string $key
75
     * @param string $key
-
 
76
     * @param mixed $value
66
     * @param mixed $value
77
     * @param int $ttl
67
     */
78
     */
68
    public function setItem($key, $value)
79
    public function setItem($key, $value, $ttl = 0)
-
 
80
    {
-
 
81
        $ttl = $ttl ? $ttl : $this->ttl;
Línea 69... Línea 82...
69
    {
82
        $key = $this->namespace ? $this->namespace . '-' . $key : $key;
Línea 70... Línea 83...
70
        
83
        
71
        if($this->cache->checkKey($key)) {
84
        if($this->hasItem($key)) {
72
            
85
            
73
            $this->cache->replace($key, serialize($value), $this->ttl);
86
            $this->cache->replace($key, serialize($value), time() + $ttl);
74
        } else {
-
 
75
            $this->cache->add($key, serialize($value), $this->ttl);
-
 
76
        }
87
        } else {
77
     
-
 
78
  
88
            $this->cache->add($key, serialize($value), time() + $ttl);
79
    }
-
 
80
    
89
        }
81
  
90
    }
82
    
91
 
83
    /**
92
    /**
84
     *
93
     *
85
     * @param string $key
94
     * @param string $key
86
     * @return boolean
95
     * @return boolean
87
     */
96
     */
-
 
97
    public function removeItem($key)
-
 
98
    {
88
    public function removeItem($key)
99
        if($this->hasItem($key)) {
89
    {
100
            $key = $this->namespace ? $this->namespace . '-' . $key : $key;
90
        if($this->cache->checkKey($key)) {
101
            
91
            return $this->cache->delete($key);
102
            return $this->cache->delete($key);
92
        } else {
103
        } else {
Línea 93... Línea -...
93
            return true;
-
 
94
        }
-
 
95
    }
-
 
96
    
-
 
97
    
104
            return true;
98
    
105
        }
99
    
106
    }
-
 
107
    
-
 
108
    /**
-
 
109
     *
-
 
110
     * {@inheritDoc}
-
 
111
     * @see \LeadersLinked\Cache\CacheInterface::getAll()
-
 
112
     */
-
 
113
    public function getAll()
-
 
114
    {
-
 
115
        return $this->cache->fetchAll();
100
    
116
    }
-
 
117
 
101
    /**
118
    /**
102
     *
119
     * 
103
     * @param string $key
120
     * {@inheritDoc}
104
     * @return boolean
121
     * @see \LeadersLinked\Cache\CacheInterface::hasItem()
Línea -... Línea 122...
-
 
122
     */
-
 
123
    public function hasItem($key)
-
 
124
    {
-
 
125
        $key = $this->namespace ? $this->namespace . '-' . $key : $key;
-
 
126
        
105
     */
127
        $keys = $this->cache->getAllKeys();
106
    public function hasItem($key)
-
 
107
    {
128
        if ($keys === false) {
108
        return $this->cache->checkKey($key);
129
            return false;
109
        
130
        }
110
    }
131
        return in_array($key, $keys, true);
111
    
132
    }
112
    
133
 
113
    /**
134
    /**
114
     *
135
     * 
115
     * @param string $key
136
     * {@inheritDoc}
-
 
137
     * @see \LeadersLinked\Cache\CacheInterface::getItem()
-
 
138
     */
116
     * @return mixed
139
    public function getItem($key)
117
     */
140
    {
118
    public function getItem($key)
141
        if($this->hasItem($key)) {
119
    {
142
            $key = $this->namespace ? $this->namespace . '-' . $key : $key;
120
        if($this->cache->checkKey($key)) {
143
            
121
            $value = $this->cache->get($key);
144
            $value = $this->cache->get($key);
122
            if(is_string($value)) {
145
            if(is_string($value)) {
123
                return unserialize($value);
146
                return unserialize($value);
124
            } else {
147
            } else {
125
                return $value;
-
 
126
            }
148
                return $value;
Línea 127... Línea -...
127
        } else {
-
 
128
            return null;
-
 
129
        }
149
            }
Línea -... Línea 150...
-
 
150
        } else {