<commit_before><?php
/*
 * Limb PHP Framework
 *
 * @link http://limb-project.com
 * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
 * @license    LGPL http://www.gnu.org/copyleft/lesser.html
 */
lmb_require('limb/cache/src/lmbCacheBackend.interface.php');
lmb_require('limb/core/src/lmbSerializable.class.php');
lmb_require('limb/fs/src/lmbFs.class.php');

/**
 * class lmbCacheFileBackend.
 *
 * @package cache
 * @version $Id$
 */
class lmbCacheFileWithMetaBackend implements lmbCacheBackend
{
  protected $_cache_dir;

  function __construct($cache_dir)
  {
    $this->_cache_dir = lmbFs::normalizePath($cache_dir);

    lmbFs :: mkdir($this->_cache_dir);
  }

  function getCacheDir()
  {
    return $this->_cache_dir;
  }

  function add($key, $value, $params = array())
  {
    $file = $this->getCacheDir() . '/' . $this->_getCacheFileName($key, $params);
    if (file_exists($file))
      return false;

    if(array_key_exists("ttl", $params))
    {
    	$meta=array('ttl' => $params['ttl']+time());
    	$this->_setMetaData($key,$meta);
    }

    if (array_key_exists("raw", $params))
    {
      lmbFs :: safeWrite($file, $value);
      return true;
    }
    else
    {
      $container = new lmbSerializable($value);
      lmbFs :: safeWrite($file, serialize($container));
      return true;
    }
  }

  function set($key, $value, $params = array())
  {
    $this->delete($key);

    $file = $this->getCacheDir() . '/' . $this->_getCacheFileName($key);

   if(isset($params['ttl']))
    {
    	$meta=array('ttl' => $params['ttl']+time());
    	$this->_setMetaData($key,$meta);
    }

    if (array_key_exists("raw", $params))
    {
      lmbFs :: safeWrite($file, $value);
      return true;
    }
    else
    {
      $container = new lmbSerializable($value);
      lmbFs :: safeWrite($file, serialize($container));
      return true;
    }
  }

  function get($key, $params = array())
  {
    if (!$file = $this->_findCacheFile($key))
      return false;

    if($meta=$this->_getMetaData($key) and isset($meta['ttl']))
    {
    	if ($meta['ttl'] - time() < 0)
        return false;
    }

    if (array_key_exists("raw", $params))
    {
      return file_get_contents($file);
    }
    else
    {
      $container = unserialize(file_get_contents($file));
      return $container->getSubject();
    }
  }

  function delete($key, $params = array())
  {
    $this->_removeFileCache($key);
  }

  function flush()
  {
    $this->_removeFileCache();
  }

  function stat($params = array())
  {
    return array();
  }

  protected function _removeFileCache($key = false)
  {
    if($key === false)<commit_msg>[svn r6920] --Now property raw is stored in file $key.meta<commit_after><?php
/*
 * Limb PHP Framework
 *
 * @link http://limb-project.com
 * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
 * @license    LGPL http://www.gnu.org/copyleft/lesser.html
 */
lmb_require('limb/cache/src/lmbCacheBackend.interface.php');
lmb_require('limb/core/src/lmbSerializable.class.php');
lmb_require('limb/fs/src/lmbFs.class.php');

/**
 * class lmbCacheFileBackend.
 *
 * @package cache
 * @version $Id$
 */
class lmbCacheFileWithMetaBackend implements lmbCacheBackend
{
  protected $_cache_dir;

  function __construct($cache_dir)
  {
    $this->_cache_dir = lmbFs::normalizePath($cache_dir);

    lmbFs :: mkdir($this->_cache_dir);
  }

  function getCacheDir()
  {
    return $this->_cache_dir;
  }

  function add($key, $value, $params = array())
  {
    $file = $this->getCacheDir() . '/' . $this->_getCacheFileName($key, $params);
    if (file_exists($file))
      return false;

    return $this->_doSet($key,$value,$file, $params);

  }

  function set($key, $value, $params = array())
  {
    $this->delete($key);

    $file = $this->getCacheDir() . '/' . $this->_getCacheFileName($key);

    return $this->_doSet($key,$value,$file, $params);
  }

  function _doSet($key,$value,$file, $params)
  {
  	if(isset($params['ttl']))
    {
    	$meta['ttl']=$params['ttl']+time();
    }
  	if(isset($params['raw'])||in_array('raw',$params))
    {
    	$meta['raw']=true;
    }
    if(isset($meta) and is_array($meta))
    {
      $this->_setMetaData($key,$meta);
    }

    if (isset($meta['raw']))
    {
      lmbFs :: safeWrite($file, $value);
      return true;
    }
    else
    {
      $container = new lmbSerializable($value);
      lmbFs :: safeWrite($file, serialize($container));
      return true;
    }
  }

  function get($key, $params = array())
  {
    if (!$file = $this->_findCacheFile($key))
      return false;

    if($meta=$this->_getMetaData($key) and isset($meta['ttl']))
    {
    	if ($meta['ttl'] - time() < 0)
        return false;
    }

    if (isset($meta['raw']))
    {
      return file_get_contents($file);
    }
    else
    {
      $container = unserialize(file_get_contents($file));
      return $container->getSubject();
    }
  }

  function delete($key, $params = array())
  {
    $this->_removeFileCache($key);
  }

  function flush()
  {
    $this->_removeFileCache();
  }

  function stat($params = array())
  {
    return array();
  }

  protected function _removeFileCache($key = false)
  {
    if($key === false)
{'fraction_non_alphanumeric': 0.1837557603686636, 'fraction_numerical': 0.004224270353302612, 'mean_word_length': 3.108044164037855, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '974532', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1814, 'n_words': 299}