Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6898 | 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
{
    
    private $memcache;
    
    
    /**
     * 
     * @var int
     */
    private $ttl;
    
    
    /**
     *
     * @var string
     */
    private $host;
    
    
    /**
     *
     * @var int
     */
    private $port;

    /**
     *
     * @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->memcache = new \Memcached();
            $this->memcache->addServer($this->host, $this->port);
        } else  if(class_exists(\Memcache::class)) {
            $this->memcache = new \Memcache();
            $this->memcache->addserver($this->host, $this->port);
        }
    }
    
    /**
     * 
     * @param string $key
     * @param mixed $value
     */
    public function setItem($key, $value)
    {
        return $this->memcache->add($key, $value, $this->ttl);
    }
    
    /**
     * 
     * @param string $key
     * @return boolean
     */
    public function touch($key)
    {
        return $this->memcache->touch($key, $this->ttl);
    }
    
    
    /**
     *
     * @param string $key
     * @return boolean
     */
    public function removeItem($key)
    {
        return $this->memcache->delete($key);
    }
    
    
    
    /**
     *
     * @param string $key
     * @return boolean
     */
    public function hasItem($key)
    {
        
        $value = $this->memcache->get($key);
        if($value === \Memcached::RES_NOTFOUND) {
            return false;
        } else {
            return true;
        }
    }
    
    
    /**
     *
     * @param string $key
     * @return mixed
     */
    public function getItem($key)
    {

        
        $value = $this->memcache->get($key);
        if($value === \Memcached::RES_NOTFOUND) {
            return false; 
        } else {
            return $value;
        }
    }
    
    
    /**
     *
     * @return bool
     */
    public function available()
    {
        return $this->memcache ? true : false;
    }

}