Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16768 | Rev 16985 | 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;

class CacheImpl implements CacheInterface
{
    
    private $memcached;

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

}