<?php
namespace OfficeBrain\Bundle\CoreBundle\Cache;

use Predis\Client as baseRedis;

class Redis {

    private $redisCache;
    private $cacheEnable;
    private $cachePrefix;
    
    public function __construct(baseRedis $redisCache, $cacheEnable=0,$ttl=3600, $cachePrefix='') {
      $this -> redisCache = $redisCache;
      $this -> cacheEnable = $cacheEnable;
      $this -> cachePrefix = $cachePrefix;
      $this -> ttl = $ttl;
    }
    
    public function getCache()
    {
        return $this->redisCache;
    }
    
    public function removeAllCacheOfProject() {
      $item = $this -> redisCache -> getItem($this -> cachePrefix);
  
    }
  
    public function removeChildCacheOfProject($key) {
      //echo "=========removeChildCacheOfProject===================".$this -> cachePrefix . ":" . $key . "*"."==========================";
      $item = $this -> redisCache -> keys($this -> cachePrefix . ":" . $key . "*");
      //print_r($item);
      foreach ($item as $k => $val) {
        $this->removePrifixByKey($val);
        $this -> redisCache -> del($val);
      }
      //exit;
    }
    
    public function removeChildCacheOfDatabase($key) {
      //echo "================$key*=============";
      $item = $this -> redisCache -> keys($key . "*");
      //print_r($item);
      foreach ($item as $k => $val) {
          $this->removePrifixByKey($val);
          $this -> redisCache -> del($val);
      }
    }
    
    public function removeCacheByPrefix($key) {
      $item = $this -> redisCache -> keys("*".$key."*");
      //print_r($item);
      foreach ($item as $k => $val) {
          $this->removePrifixByKey($val);
          $this -> redisCache -> del($val);
      }
    }
    
    public function removePrifixByKey(&$key) {
        $option=$this->redisCache;
        $prifix=($option->getConnection()->getParameters()->__get('prefix'));
        if(!empty($prifix)) {
            $key=str_replace($prifix, '', $key);
        }
    }
    
    public function getCacheByPrefix($key) {
      return $this -> redisCache -> keys("*".$key."*");
    }

    public function removeAllCache($key) {
      //echo "===============".$this -> cachePrefix . ":" . $key."===============";
      $item = $this -> redisCache -> del($this -> cachePrefix . ":" . $key);
      //$item->clear();
      
    }
  
    public function setCacheData($key, $data) {
      if (!$this -> cacheEnable) {
        return false;
      }
      $this -> redisCache -> set($this -> cachePrefix . ":" . $key, serialize($data));
      if($this -> ttl>0)
      {
          $this -> redisCache ->expire($this -> cachePrefix . ":" . $key,$this -> ttl);
      }
    }
  
    public function getCacheData($key) {
      if (!$this -> cacheEnable) {
        return false;
      }
  
      if (!$this -> redisCache -> exists($this -> cachePrefix . ":" . $key)) {
          
        return false;
      }
      $data = unserialize($this -> redisCache -> get($this -> cachePrefix . ":" . $key));
  
      if (!empty($data)) {
        return $data;
      }
  
      return false;
    }
  
    public function getCacheDataWithoutPrifix($key) {
      if (!$this -> cacheEnable) {
        return false;
      }
      
      $this->removePrifixByKey($key);
      if (!$this -> redisCache -> exists($key)) {
        return false;
      }
      $data = unserialize($this -> redisCache -> get($key));
  
      if (!empty($data)) {
        return $data;
      }
      return false;
    }
  
    public function createKey($function_args) {
      $function_args = (array)$function_args;
      return implode(":", $function_args);
    }
    public function removeCacheWithPrifix($moduleCachePrefix){
    	//echo "=========removeChildCacheOfProject===================".$this -> cachePrefix . ":" . $key . "*"."==========================";
    	$item = $this -> redisCache -> keys("*");
    
    	foreach ($item as $k => $val) {
    		$this->removePrifixByKey($val);
    		$this -> redisCache -> del($val);
    	}
    	 
    	return true;
    	//exit;
    }
}
