<?php

class CacheRam
{

    private $mem;
    private $expires;
    private $prefix;
    private $idconnect = '127.0.0.1';

    public function __construct()
    {
        $this->mem = new Memcache();
        if(defined('CACHE_SERVER')){
            $this->idconnect = CACHE_SERVER;
        }        
        $this->mem->pconnect($this->idconnect, 11211);
        $this->expires = 3600; //tempo padrão para expirar cache....
    }

    public function setPrefix($prefix)
    {
        $this->prefix = md5($prefix);
    }

    public function get($key)
    {
        $keyMem = $this->prefix . $key;

        $cache = $this->mem->get($keyMem);

        if ($cache !== false) {
            return $cache;
        } else {
            return false;
        }
    }

    public function set($key, $value, $timeCache = false)
    {
        if (!$timeCache) {
            $timeCache = $this->expires;
        }

        $keyMem = $this->prefix . $key;
        $this->mem->set($keyMem, $value, MEMCACHE_COMPRESSED, $timeCache);
    }

    public function delete($key)
    {
        //
    }

    public function close()
    {
        $this->mem->close();
    }

}
