<?php
/**
 * @copyright NetMonsters <team@netmonsters.ru>
 * @link http://netmonsters.ru
 * @package Majestic
 * @subpackage Cache
 * @since 2010-03-10
 * @version SVN: $Id$
 * @filesource $URL$
 */

class CacheKey
{
    
    protected $key;
    protected $params = '';
    protected $expire = 0;
    
    /**
     * @param string $key
     * @param mixed $params
     * @param iCacheable $cacheable
     * @return CacheKey
     */
    public function __construct($key, $params = array(), $cacheable)
    {
        $this->key = $key;
        if (!$cacheable instanceof iCacheable) {
            throw new GeneralException('CacheKey depends on iCacheable instance');
        }
        $this->cache  = $cacheable->getCache();
        $this->expire = $cacheable->getKeyExpire($this->key);
        $this->params = (is_array($params)) ? implode('', $params) : $params;
    }
    
    protected function getCacheKey()
    {
        $params = ($this->params) ? ('_' . $this->params) : '';
        return $this->key . $params;
    }
    
    protected function getExpire()
    {
        return $this->expire;
    }
    
    /**
     * @param int $expire
     */
    public function setExpire($expire)
    {
        $this->expire = $expire;
    }
    
    public function get()
    {
        return $this->cache->get($this->getCacheKey());        
    }
    
    /**
     * @param mixed $value
     */
    public function set($value)
    {
        return $this->cache->set($this->getCacheKey(), $value, $this->expire);
    }
    
    public function del()
    {
        return $this->cache->del($this->getCacheKey());
    }
}