Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 16785 Rev 16985
Línea 2... Línea 2...
2
 
2
 
Línea 3... Línea 3...
3
declare(strict_types=1);
3
declare(strict_types=1);
Línea -... Línea 4...
-
 
4
 
-
 
5
namespace LeadersLinked\Cache;
4
 
6
 
5
namespace LeadersLinked\Cache;
7
use function PHPUnit\Framework\fileExists;
6
 
-
 
7
class CacheImpl implements CacheInterface
-
 
8
{
-
 
9
    /**
-
 
10
     * 
-
 
11
     * @var \Memcached|\Memcache
-
 
12
     */
-
 
13
    private $memcached;
8
 
14
 
-
 
15
    
-
 
16
    
-
 
17
    /**
-
 
18
     * 
-
 
19
     * @var int
-
 
20
     */
-
 
21
    private $ttl;
-
 
22
    
-
 
23
    
-
 
24
    /**
-
 
25
     *
-
 
26
     * @var string
-
 
27
     */
-
 
28
    private $host;
-
 
29
    
-
 
30
    
-
 
31
    /**
-
 
32
     *
-
 
33
     * @var int
-
 
34
     */
-
 
35
    private $port;
9
class CacheImpl implements CacheInterface
Línea 36... Línea 10...
36
    
10
{
37
    
11
 
38
    
12
     
39
    
13
    
40
    /**
14
    /**
41
     *
15
     *
-
 
16
     * @param Array $config
42
     * @param Array $config
17
     */
43
     */
18
    public function __construct($config)
44
    public function __construct($config)
19
    {
Línea 52... Línea 27...
52
            $this->memcached = new \Memcached();
27
            $this->memcached = new \Memcached();
53
            $this->memcached->addServer($this->host, $this->port);
28
            $this->memcached->addServer($this->host, $this->port);
54
        } else  if(class_exists(\Memcache::class)) {
29
        } else  if(class_exists(\Memcache::class)) {
55
            $this->memcached = new \Memcache();
30
            $this->memcached = new \Memcache();
56
            $this->memcached->addserver($this->host, $this->port);
31
            $this->memcached->addserver($this->host, $this->port);
57
        }
32
        }*/
Línea 58... Línea 33...
58
 
33
 
Línea 59... Línea 34...
59
 
34
 
Línea 64... Línea 39...
64
     * @param string $key
39
     * @param string $key
65
     * @param mixed $value
40
     * @param mixed $value
66
     */
41
     */
67
    public function setItem($key, $value)
42
    public function setItem($key, $value)
68
    {
43
    {
69
        if($this->memcached) {
44
        $filename = $this->getFilename($key);
70
            $this->memcached->add($key, $value, $this->ttl);
-
 
71
        } else {
-
 
72
            $filename = $this->getFilename($key);
45
        if(file_exists($filename)) {
73
            return file_put_contents($filename, serialize($value));
46
            @unlink($filename);
74
        }
47
        }
-
 
48
        
-
 
49
        return file_put_contents($filename, serialize($value));
-
 
50
        
75
    }
51
    }
Línea 76... Línea 52...
76
    
52
    
77
    /**
53
    /**
78
     * 
54
     * 
79
     * @param string $key
55
     * @param string $key
80
     * @return boolean
56
     * @return boolean
81
     */
57
     */
82
    public function touch($key)
58
    public function touch($key)
-
 
59
    {
-
 
60
 
83
    {
61
        $filename = $this->getFilename($key);
84
        if($this->memcached) {
62
        if(file_exists($filename)) {
85
            return $this->memcached->touch($key, $this->ttl);
63
            return touch($filename);
86
        } else {
-
 
87
            $filename = $this->getFilename($key);
-
 
88
            if(file_exists($filename)) {
-
 
89
                return touch($filename);
-
 
90
            } else {
64
        } else {
91
                return true;
-
 
92
            }
65
            return true;
93
        }
66
        }
Línea 94... Línea 67...
94
    }
67
    }
Línea 99... Línea 72...
99
     * @param string $key
72
     * @param string $key
100
     * @return boolean
73
     * @return boolean
101
     */
74
     */
102
    public function removeItem($key)
75
    public function removeItem($key)
103
    {
76
    {
-
 
77
        $filename = $this->getFilename($key);
104
        if($this->memcached) {
78
        if(file_exists($filename)) {
105
            return $this->memcached->delete($key);
79
            return unlink($filename);
106
        } else {
80
        } else {
107
            $filename = $this->getFilename($key);
-
 
108
            if(file_exists($filename)) {
-
 
109
                return unlink($filename);
-
 
110
            } else {
-
 
111
                return true;
81
            return true;
112
            }
-
 
113
        }
82
        }
114
    }   
83
    }   
Línea -... Línea 84...
-
 
84
    
-
 
85
   
Línea 115... Línea 86...
115
    
86
    
116
    
87
    
117
    
88
    
118
    /**
89
    /**
119
     *
90
     *
120
     * @param string $key
91
     * @param string $key
121
     * @return boolean
92
     * @return boolean
122
     */
-
 
123
    public function hasItem($key)
-
 
124
    {
-
 
125
        if($this->memcached) {
-
 
126
        
-
 
127
            $value = $this->memcached->get($key);
-
 
128
            if($value === \Memcached::RES_NOTFOUND) {
-
 
129
                return false;
-
 
130
            } else {
-
 
131
                return empty($value) ? false : true;
93
     */
132
            }
94
    public function hasItem($key)
133
        } else {
-
 
Línea 134... Línea 95...
134
            $filename = $this->getFilename($key);
95
    {
Línea 135... Línea 96...
135
            return file_exists($filename);
96
        $filename = $this->getFilename($key);
Línea 143... Línea 104...
143
     * @param string $key
104
     * @param string $key
144
     * @return mixed
105
     * @return mixed
145
     */
106
     */
146
    public function getItem($key)
107
    public function getItem($key)
147
    {
108
    {
148
        if($this->memcached) {
-
 
149
        
-
 
150
            $value = $this->memcached->get($key);
109
        $filename = $this->getFilename($key);
151
            if($value === \Memcached::RES_NOTFOUND) {
-
 
152
                return false; 
110
        if(file_exists($filename)) {
153
            } else {
-
 
154
                return $value;
111
            return unserialize(file_get_contents($filename)); 
155
            }
-
 
156
        } else {
112
        } else {
157
            $filename = $this->getFilename($key);
-
 
158
            if(file_exists($filename)) {
-
 
159
                return unserialize(file_get_contents($filename)); 
-
 
160
            } else {
-
 
161
                return null;
113
            return null;
162
            }
-
 
163
            
-
 
164
            
-
 
165
        }
114
        }
-
 
115
 
166
    }
116
    }
Línea 167... Línea 117...
167
    
117
    
168
    /**
118
    /**
169
     * 
119
     * 
Línea 178... Línea 128...
178
        }
128
        }
Línea 179... Línea 129...
179
        
129
        
180
        return $filepath . DIRECTORY_SEPARATOR . $key . '.json';
130
        return $filepath . DIRECTORY_SEPARATOR . $key . '.json';
Línea -... Línea 131...
-
 
131
    }
-
 
132
 
-
 
133
    
-
 
134
    
-
 
135
    /**
-
 
136
     *
-
 
137
     * @return bool
-
 
138
     */
-
 
139
    public function available()
-
 
140
    {
-
 
141
        return  true;
181
    }
142
    }