<commit_before><?php
/*
 * This file is part of the psToolboxPlugin package.
 * (c) 2009-2010 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 *
 * @package    psToolboxPlugin
 * @author     <NAME>
 * @author     <NAME>
 * 
 * @version    SVN: $Id$
 */
class psNamedCache
{
  static protected $instance = null;
  protected $cache = null;
  
  static protected function getInstance()
  {
    if (!isset(self::$instance))
    {
      self::$instance = new psNamedCache();
      self::$instance->initialize();
    }
    
    return self::$instance;
  }
  
  protected function initialize()
  {
    $config = sfConfig::get('ps_toolbox_named_cache', array());
    
    $options = array(
      'class'   => 'sfNoCache',
      'options' => array()
    );
    
    $options = array_merge($options, isset($config['options']) ? $config : array());
    
    $this->cache = new $options['class'];
    $this->cache->initialize($options['options']);
  }
  
  protected function getCache()
  {
    return $this->cache;
  }
  
  static protected function generateKey($name = '', $params = array())
  {
    $params_string = array();

    // Build parameters string "a la" symfony routing
    if (sizeof($params) > 0)
    {
      foreach ($params as $param => $value)
      {
        $params_string[]= sprintf('%s/%s', $param, $value);
      }
    }

    $key = $name. (sizeof($params) > 0 ? '/'. implode('/', $params_string) : '');

    return $key;
  }

  static public function get($name, $default = null, $params = array())
  {   
    $key = self::generateKey($name, $params);
    
    return self::getInstance()->getCache()->get($key, $default);
  }
  
  static public function getQueryResult($query, $name, $params = array(), $lifetime = null)
  {
    if (!is_object($query) || !($query instanceof Doctrine_Query))
    {
      throw new sfException('invalid query object');
    }
    
    $key = self::generateKey($name, $params);
    $defaultValue = '#%@psNamedCacheDefaultValue@%#';
    $instance = self::getInstance();
    $data = $instance->getCache()->get($key, $defaultValue);

    if ($data===$defaultValue)
    {
      $result = $query->execute();
      $instance->getCache()->set($key, array($query->getRoot()->getComponentName() => $result->toArray(true)), $lifetime);

      return $result;
    }
    
    $keys = array_keys($data);
    $result = new Doctrine_Collection($keys[0]);
    $result->fromArray($data[$keys[0]], true);
    
    return $result;
  }
  
  static public function has($name, $params = array())
  {
    $key = self::generateKey($name, $params);
    return self::getInstance()->getCache()->has($key);
  }
  
  static public function set($name, $data, $params = array(), $lifetime = null)
  {
    $key = self::generateKey($name, $params);
    return self::getInstance()->getCache()->set($key, $data, $lifetime);
  }
  
  static public function remove($name, $params = array())
  {
    $key = self::generateKey($name, $params);
    return self::getInstance()->getCache()->remove($key);
  }
	
  static public function removePattern($pattern)
  {
    return self::getInstance()->getCache()->removePattern($pattern);	
  }
    
  static public function clean($mode = sfCache::ALL)
  {
    return self::getInstance()->getCache()->clean($mode);
  }
  
  static public function getTimeout($name, $params = array())
  {
    $key = self::generateKey($name, $params);<commit_msg>fixed a serialize problem<commit_after><?php
/*
 * This file is part of the psToolboxPlugin package.
 * (c) 2009-2010 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 *
 * @package    psToolboxPlugin
 * @author     <NAME>
 * @author     <NAME>
 * 
 * @version    SVN: $Id$
 */
class psNamedCache
{
  static protected $instance = null;
  protected $cache = null;
  
  static protected function getInstance()
  {
    if (!isset(self::$instance))
    {
      self::$instance = new psNamedCache();
      self::$instance->initialize();
    }
    
    return self::$instance;
  }
  
  protected function initialize()
  {
    $config = sfConfig::get('ps_toolbox_named_cache', array());
    
    $options = array(
      'class'   => 'sfNoCache',
      'options' => array()
    );
    
    $options = array_merge($options, isset($config['options']) ? $config : array());
    
    $this->cache = new $options['class']($options['options']);
  }
  
  protected function getCache()
  {
    return $this->cache;
  }
  
  static protected function generateKey($name = '', $params = array())
  {
    $params_string = array();

    // Build parameters string "a la" symfony routing
    if (sizeof($params) > 0)
    {
      foreach ($params as $param => $value)
      {
        $params_string[]= sprintf('%s/%s', $param, $value);
      }
    }

    $key = $name. (sizeof($params) > 0 ? '/'. implode('/', $params_string) : '');

    return $key;
  }

  static public function get($name, $default = null, $params = array())
  {   
    $key = self::generateKey($name, $params);

    $data = self::getInstance()->getCache()->get($key, '[[[DEFAULT]]]');
    
    return ('[[[DEFAULT]]]' === $data) ? $default : unserialize($data);
  }
  
  static public function getQueryResult($query, $name, $params = array(), $lifetime = null)
  {
    if (!is_object($query) || !($query instanceof Doctrine_Query))
    {
      throw new sfException('invalid query object');
    }
    
    $key = self::generateKey($name, $params);
    $defaultValue = '#%@psNamedCacheDefaultValue@%#';
    $instance = self::getInstance();
    $data = $instance->getCache()->get($key, $defaultValue);

    if ($data===$defaultValue)
    {
      $result = $query->execute();
      $instance->getCache()->set($key, array($query->getRoot()->getComponentName() => $result->toArray(true)), $lifetime);

      return $result;
    }
    
    $keys = array_keys($data);
    $result = new Doctrine_Collection($keys[0]);
    $result->fromArray($data[$keys[0]], true);
    
    return $result;
  }
  
  static public function has($name, $params = array())
  {
    $key = self::generateKey($name, $params);
    return self::getInstance()->getCache()->has($key);
  }
  
  static public function set($name, $data, $params = array(), $lifetime = null)
  {
    $key = self::generateKey($name, $params);
    return self::getInstance()->getCache()->set($key, serialize($data), $lifetime);
  }
  
  static public function remove($name, $params = array())
  {
    $key = self::generateKey($name, $params);
    return self::getInstance()->getCache()->remove($key);
  }
	
  static public function removePattern($pattern)
  {
    return self::getInstance()->getCache()->removePattern($pattern);	
  }
    
  static public function clean($mode = sfCache::ALL)
  {
    return self::getInstance()->getCache()->clean($mode);
  }
  
  static public function getTimeout($name, $params = array())
  {
    $key = self::generateKey($name, $params);
{'fraction_non_alphanumeric': 0.1793416572077185, 'fraction_numerical': 0.00340522133938706, 'mean_word_length': 3.2799028536733457, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1188642', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2165, 'n_words': 494}