Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 16985 Rev 17002
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 4... Línea -...
4
 
-
 
Línea 5... Línea 4...
5
namespace LeadersLinked\Cache;
4
 
6
 
5
namespace LeadersLinked\Cache;
7
use function PHPUnit\Framework\fileExists;
6
 
-
 
7
 
-
 
8
class CacheImpl implements CacheInterface
-
 
9
{
-
 
10
    
-
 
11
    /**
-
 
12
     * 
-
 
13
     * @var \LeadersLinked\Cache\CacheInterface
-
 
14
     */
-
 
15
    private static $_instance;
-
 
16
    
-
 
17
    
-
 
18
    /**
-
 
19
     * 
-
 
20
     * @var \Memcached
-
 
21
     */
-
 
22
    private $cache;
-
 
23
    
-
 
24
    /**
8
 
25
     * 
Línea 9... Línea 26...
9
class CacheImpl implements CacheInterface
26
     * @var int
10
{
27
     */
11
 
28
    private $ttl;
12
     
29
 
13
    
30
    
14
    /**
31
    /**
15
     *
32
     *
16
     * @param Array $config
33
     * @param Array $config
17
     */
34
     */
18
    public function __construct($config)
35
    private function __construct($config)
-
 
36
    {
-
 
37
  
-
 
38
         $this->ttl         = intval($config['leaderslinked.memcache.ttl'], 10);
-
 
39
         $host              = strval($config['leaderslinked.memcache.host']);
-
 
40
         $port              = intval($config['leaderslinked.memcache.port'], 10);
Línea 19... Línea -...
19
    {
-
 
20
        /*
-
 
21
        $this->ttl    = intval($config['leaderslinked.memcache.ttl'], 10);
-
 
22
        $this->host   = strval($config['leaderslinked.memcache.host']);
-
 
23
        $this->port   = intval($config['leaderslinked.memcache.port'], 10);
-
 
24
        
-
 
25
        
-
 
26
        if(class_exists(\Memcached::class)) {
-
 
27
            $this->memcached = new \Memcached();
-
 
28
            $this->memcached->addServer($this->host, $this->port);
41
         
Línea 29... Línea 42...
29
        } else  if(class_exists(\Memcache::class)) {
42
 
30
            $this->memcached = new \Memcache();
43
         $this->cache = new \Memcached();
31
            $this->memcached->addserver($this->host, $this->port);
44
         $this->cache->addserver($host, $port);
32
        }*/
45
         
33
 
46
        
34
 
47
        
35
    }
48
    }
36
    
-
 
37
    /**
49
    
38
     * 
50
    /**
39
     * @param string $key
51
     * 
40
     * @param mixed $value
-
 
41
     */
52
     * @param array $config
42
    public function setItem($key, $value)
-
 
43
    {
53
     * @return \LeadersLinked\Cache\CacheInterface
Línea 44... Línea 54...
44
        $filename = $this->getFilename($key);
54
     */
45
        if(file_exists($filename)) {
55
    public static function getInstance($config)
46
            @unlink($filename);
56
    {
47
        }
57
        if(self::$_instance == null) {
48
        
58
            self::$_instance = new CacheImpl($config);
49
        return file_put_contents($filename, serialize($value));
59
        }
50
        
60
        return self::$_instance;
51
    }
61
    }
52
    
62
    
53
    /**
-
 
-
 
63
    /**
54
     * 
64
     *
55
     * @param string $key
65
     * @param string $key
56
     * @return boolean
66
     * @param mixed $value
57
     */
67
     */
-
 
68
    public function setItem($key, $value)
-
 
69
    {
58
    public function touch($key)
70
        
Línea -... Línea 71...
-
 
71
        if($this->cache->checkKey($key)) {
Línea 59... Línea 72...
59
    {
72
            
60
 
73
            $this->cache->replace($key, serialize($value), $this->ttl);
61
        $filename = $this->getFilename($key);
74
        } else {
62
        if(file_exists($filename)) {
75
            $this->cache->add($key, serialize($value), $this->ttl);
63
            return touch($filename);
76
        }
64
        } else {
77
     
65
            return true;
78
  
66
        }
79
    }
67
    }
-
 
68
    
80
    
69
    
81
  
70
    /**
82
    
71
     *
83
    /**
72
     * @param string $key
84
     *
-
 
85
     * @param string $key
Línea 73... Línea -...
73
     * @return boolean
-
 
Línea 74... Línea 86...
74
     */
86
     * @return boolean
75
    public function removeItem($key)
87
     */
76
    {
88
    public function removeItem($key)
77
        $filename = $this->getFilename($key);
89
    {
78
        if(file_exists($filename)) {
90
        if($this->cache->checkKey($key)) {
79
            return unlink($filename);
91
            return $this->cache->delete($key);
80
        } else {
92
        } else {
81
            return true;
93
            return true;
82
        }
-
 
83
    }   
94
        }
84
    
95
    }
Línea 85... Línea 96...
85
   
96
    
86
    
97
    
87
    
98
    
88
    
99
    
89
    /**
100
    
90
     *
101
    /**
91
     * @param string $key
102
     *
-
 
103
     * @param string $key
92
     * @return boolean
104
     * @return boolean
93
     */
105
     */
94
    public function hasItem($key)
106
    public function hasItem($key)
-
 
107
    {
-
 
108
        return $this->cache->checkKey($key);
-
 
109
        
95
    {
110
    }
96
        $filename = $this->getFilename($key);
111
    
97
        return file_exists($filename);
112
    
98
            
-
 
99
    }
-
 
100
    
-
 
101
    
-
 
102
    /**
-
 
103
     *
-
 
104
     * @param string $key
-
 
105
     * @return mixed
-
 
106
     */
-
 
107
    public function getItem($key)
-
 
108
    {
-
 
109
        $filename = $this->getFilename($key);
-
 
110
        if(file_exists($filename)) {
-
 
111
            return unserialize(file_get_contents($filename)); 
-
 
Línea 112... Línea -...
112
        } else {
-
 
113
            return null;
-
 
114
        }
-
 
115
 
-
 
116
    }
-
 
117
    
-
 
118
    /**
-
 
119
     * 
-
 
120
     * @param string $key
-
 
121
     * @return string
-
 
122
     */
-
 
123
    private function getFilename($key) 
-
 
124
    {
113
    /**
Línea -... Línea 114...
-
 
114
     *
-
 
115
     * @param string $key
125
        $filepath = 'data' . DIRECTORY_SEPARATOR . 'cache';
116
     * @return mixed