Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev 192 Rev 283
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
 
-
 
5
namespace LeadersLinked\Cache;
-
 
Línea 6... Línea 4...
6
 
4
 
7
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
5
namespace LeadersLinked\Cache;
Línea -... Línea 6...
-
 
6
 
-
 
7
 
-
 
8
class CacheImpl implements CacheInterface
-
 
9
{
-
 
10
    
-
 
11
    /**
-
 
12
     * 
-
 
13
     * @var \LeadersLinked\Cache\CacheInterface
-
 
14
     */
-
 
15
    private static $_instance;
-
 
16
    
-
 
17
    
Línea -... Línea 18...
-
 
18
    /**
-
 
19
     * 
-
 
20
     * @var \Memcached
-
 
21
     */
-
 
22
    private $cache;
-
 
23
    
Línea 8... Línea 24...
8
 
24
    /**
9
 
25
     * 
10
class CacheImpl implements CacheInterface
26
     * @var int
11
{
27
     */
12
    
28
    private $ttl;
13
    
29
 
14
    
30
    
15
    /**
31
    /**
16
     *
32
     *
17
     * @param Array $config
33
     * @param Array $config
Línea -... Línea 34...
-
 
34
     */
-
 
35
    private function __construct($config)
-
 
36
    {
Línea 18... Línea -...
18
     */
-
 
19
    public function __construct($config)
-
 
20
    {
-
 
21
        /*
-
 
22
         $this->ttl    = intval($config['leaderslinked.memcache.ttl'], 10);
-
 
23
         $this->host   = strval($config['leaderslinked.memcache.host']);
-
 
24
         $this->port   = intval($config['leaderslinked.memcache.port'], 10);
-
 
Línea 25... Línea 37...
25
         
37
  
Línea 26... Línea 38...
26
         
38
         $this->ttl         = intval($config['leaderslinked.memcache.ttl'], 10);
27
         if(class_exists(\Memcached::class)) {
39
         $host              = strval($config['leaderslinked.memcache.host']);
28
         $this->memcached = new \Memcached();
40
         $port              = intval($config['leaderslinked.memcache.port'], 10);
29
         $this->memcached->addServer($this->host, $this->port);
41
         
30
         } else  if(class_exists(\Memcache::class)) {
42
 
31
         $this->memcached = new \Memcache();
43
         $this->cache = new \Memcached();
32
         $this->memcached->addserver($this->host, $this->port);
44
         $this->cache->addserver($host, $port);
33
         }*/
-
 
34
        
45
         
35
        
46
        
36
    }
47
        
37
    
-
 
38
    /**
48
    }
39
     *
-
 
40
     * @param string $key
49
    
Línea 41... Línea 50...
41
     * @param mixed $value
50
    /**
42
     */
51
     * 
43
    public function setItem($key, $value)
52
     * @param array $config
44
    {
53
     * @return \LeadersLinked\Cache\CacheInterface
45
        $filename = $this->getFilename($key);
54
     */
46
        if(file_exists($filename)) {
55
    public static function getInstance($config)
47
            @unlink($filename);
56
    {
Línea 48... Línea 57...
48
        }
57
        if(self::$_instance == null) {
49
        
-
 
-
 
58
            self::$_instance = new CacheImpl($config);
50
        return file_put_contents($filename, serialize($value));
59
        }
51
        
60
        return self::$_instance;
52
    }
61
    }
53
    
62
    
-
 
63
    /**
-
 
64
     *
54
    /**
65
     * @param string $key
Línea -... Línea 66...
-
 
66
     * @param mixed $value
Línea 55... Línea 67...
55
     *
67
     */
56
     * @param string $key
68
    public function setItem($key, $value)
57
     * @return boolean
69
    {
58
     */
70
        
59
    public function touch($key)
71
        if($this->cache->checkKey($key)) {
60
    {
72
            
61
        
73
            $this->cache->replace($key, serialize($value), $this->ttl);
62
        $filename = $this->getFilename($key);
74
        } else {
63
        if(file_exists($filename)) {
-
 
64
            return touch($filename);
75
            $this->cache->add($key, serialize($value), $this->ttl);
65
        } else {
76
        }
66
            return true;
77
     
67
        }
78
  
68
    }
79
    }
Línea 92... Línea 103...
92
     * @param string $key
103
     * @param string $key
93
     * @return boolean
104
     * @return boolean
94
     */
105
     */
95
    public function hasItem($key)
106
    public function hasItem($key)
96
    {
107
    {
97
        $filename = $this->getFilename($key);
108
        return $this->cache->checkKey($key);
98
        return file_exists($filename);
-
 
Línea 99... Línea 109...
99
        
109
        
Línea 100... Línea 110...
100
    }
110
    }
Línea 105... Línea 115...
105
     * @param string $key
115
     * @param string $key
106
     * @return mixed
116
     * @return mixed
107
     */
117
     */
108
    public function getItem($key)
118
    public function getItem($key)
109
    {
119
    {
-
 
120
        if($this->cache->checkKey($key)) {
110
        $filename = $this->getFilename($key);
121
            $value = $this->cache->get($key);
111
        if(file_exists($filename)) {
122
            if(is_string($value)) {
112
            return unserialize(file_get_contents($filename));
123
                return unserialize($value);
-
 
124
            } else {
-
 
125
                return $value;
-
 
126
            }
113
        } else {
127
        } else {
114
            return null;
128
            return null;
115
        }
129
        }
Línea 116... Línea 130...
116
        
130
        
Línea 117... Línea -...
117
    }
-
 
118
    
-
 
119
    /**
-
 
120
     *
-
 
121
     * @param string $key
-
 
122
     * @return string
-
 
123
     */
-
 
124
    private function getFilename($key)
-
 
125
    {
-
 
126
        $filepath = 'data' . DIRECTORY_SEPARATOR . 'cache';
-
 
127
        if(!file_exists($filepath)) {
-
 
128
            @mkdir($filepath, 0755);
-
 
129
        }
-
 
130
        
-
 
131
        return $filepath . DIRECTORY_SEPARATOR . $key . '.json';
131
    }
132
    }
-
 
133
    
132
    
134
    
-
 
135
    
-
 
136
    /**
-
 
137
     *
-
 
138
     * @return bool
-
 
139
     */
-
 
140
    public function available()
-
 
141
    {
-
 
142
        return  true;
133