Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | Rev 333 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php

declare(strict_types=1);

namespace LeadersLinked\Cache;

use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;


class CacheImpl implements CacheInterface
{
    
    
    
    /**
     *
     * @param Array $config
     */
    public function __construct($config)
    {
        /*
         $this->ttl    = intval($config['leaderslinked.memcache.ttl'], 10);
         $this->host   = strval($config['leaderslinked.memcache.host']);
         $this->port   = intval($config['leaderslinked.memcache.port'], 10);
         
         
         if(class_exists(\Memcached::class)) {
         $this->memcached = new \Memcached();
         $this->memcached->addServer($this->host, $this->port);
         } else  if(class_exists(\Memcache::class)) {
         $this->memcached = new \Memcache();
         $this->memcached->addserver($this->host, $this->port);
         }*/
        
        
    }
    
    /**
     *
     * @param string $key
     * @param mixed $value
     */
    public function setItem($key, $value)
    {
        $filename = $this->getFilename($key);
        if(file_exists($filename)) {
            @unlink($filename);
        }
        
        return file_put_contents($filename, serialize($value));
        
    }
    
    /**
     *
     * @param string $key
     * @return boolean
     */
    public function touch($key)
    {
        
        $filename = $this->getFilename($key);
        if(file_exists($filename)) {
            return touch($filename);
        } else {
            return true;
        }
    }
    
    
    /**
     *
     * @param string $key
     * @return boolean
     */
    public function removeItem($key)
    {
        $filename = $this->getFilename($key);
        if(file_exists($filename)) {
            return unlink($filename);
        } else {
            return true;
        }
    }
    
    
    
    
    
    /**
     *
     * @param string $key
     * @return boolean
     */
    public function hasItem($key)
    {
        $filename = $this->getFilename($key);
        return file_exists($filename);
        
    }
    
    
    /**
     *
     * @param string $key
     * @return mixed
     */
    public function getItem($key)
    {
        $filename = $this->getFilename($key);
        if(file_exists($filename)) {
            return unserialize(file_get_contents($filename));
        } else {
            return null;
        }
        
    }
    
    /**
     *
     * @param string $key
     * @return string
     */
    private function getFilename($key)
    {
        $filepath = 'data' . DIRECTORY_SEPARATOR . 'cache';
        if(!file_exists($filepath)) {
            @mkdir($filepath, 0755);
        }
        
        return $filepath . DIRECTORY_SEPARATOR . $key . '.json';
    }
    
    
    
    /**
     *
     * @return bool
     */
    public function available()
    {
        return  true;
    }
}