<commit_before><?php
/**
 * Wei Framework
 *
 * @copyright   Copyright (c) 2008-2020 <NAME>
 * @license     http://opensource.org/licenses/mit-license.php MIT License
 */

namespace Wei;

/**
 * A cache service that stored data in files
 *
 * @author      <NAME> <<EMAIL>>
 */
class FileCache extends BaseCache
{
    /**
     * The cache directory
     *
     * @var string
     */
    protected $dir = 'cache';

    /**
     * Illegal chars as the name of cache, would be replaced to "_"
     *
     * @var array
     */
    protected $illegalChars = array(
        '\\', '/', ':', '*', '?', '"', '<', '>', '|', "\r", "\n"
    );

    /**
     * The cache file extension
     *
     * @var string
     */
    protected $ext = 'cache';

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        parent::__construct($options + array(
            'dir' => $this->dir
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function get($key, $expire = null, $fn = null)
    {
        if (!is_file($file = $this->getFile($key))) {
            $result = false;
        } else {
            $content = $this->getContent($file);;
            if ($content && is_array($content) && time() < $content[0]) {
                $result = $content[1];
            } else {
                $this->remove($key);
                $result = false;
            }
        }
        return $this->processGetResult($key, $result, $expire, $fn);
    }

    /**
     * {@inheritdoc}
     */
    public function set($key, $value, $expire = 0)
    {
        $file = $this->getFile($key);
        $content = $this->prepareContent($value, $expire);
        return (bool) file_put_contents($file, $content, LOCK_EX);
    }

    /**
     * {@inheritdoc}
     */
    public function remove($key)
    {
        if (is_file($file = $this->getFile($key))) {
            return unlink($file);
        } else {
            return false;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function exists($key)
    {
        if (!is_file($file = $this->getFile($key))) {
            return false;
        }

        $content = $this->getContent($file);
        if ($content && is_array($content) && time() < $content[0]) {
            return true;
        } else {
            $this->remove($key);
            return false;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function add($key, $value, $expire = 0)
    {
        $file = $this->getFile($key);

        if (!is_file($file)) {
            // Open and try to lock file immediately
            if (!$handle = $this->openAndLock($file, 'wb', LOCK_EX | LOCK_NB)) {
                return false;
            }

            $rewrite = false;
        } else {
            // Open file for reading and rewriting
            if (!$handle = $this->openAndLock($file, 'r+b', LOCK_EX)) {
                return false;
            }

            // The cache is not expired
            if ($this->readAndVerify($handle, $file)) {
                fclose($handle);
                return false;
            }

            $rewrite = true;
        }

        $content = $this->prepareContent($value, $expire);
        return $this->writeAndRelease($handle, $content, $rewrite);
    }

    /**
     * {@inheritdoc}
     */
    public function replace($key, $value, $expire = 0)
    {
        if (!is_file($file = $this->getFile($key))) {
            return false;
        }

        // Open file for reading and rewriting
        if (!$handle = $this->openAndLock($file, 'r+b', LOCK_EX)) {
            return false;
        }

        if (!$this->readAndVerify($handle, $file)) {
            fclose($handle);
            return false;
        }

        $content = $this->prepareContent($value, $expire);
        return $this->writeAndRelease($handle, $content, true);
    }

    /**
     * {@inheritdoc}
     */
    public function incr($key, $offset = 1)
    {
        $file = $this->getFile($key);

        if (!is_file($file)) {
            return $this->set($key, $offset) ? $offset : false;
        }

        // Open file for reading and rewriting
        if (!$handle = $this->openAndLock($file, 'r+b', LOCK_EX)) {
            return false;
        }

        // Prepare file content
        if (!$content = $this->readAndVerify($handle, $file)) {
            $content = $this->prepareContent($offset, 0);
            $result = $offset;
        } else {
            $result = $content[1] += $offset;<commit_msg>fix: 非数字值 incr 出错  ErrorException : A non-numeric value encountered<commit_after><?php
/**
 * Wei Framework
 *
 * @copyright   Copyright (c) 2008-2020 <NAME>
 * @license     http://opensource.org/licenses/mit-license.php MIT License
 */

namespace Wei;

/**
 * A cache service that stored data in files
 *
 * @author      <NAME> <<EMAIL>>
 */
class FileCache extends BaseCache
{
    /**
     * The cache directory
     *
     * @var string
     */
    protected $dir = 'cache';

    /**
     * Illegal chars as the name of cache, would be replaced to "_"
     *
     * @var array
     */
    protected $illegalChars = array(
        '\\', '/', ':', '*', '?', '"', '<', '>', '|', "\r", "\n"
    );

    /**
     * The cache file extension
     *
     * @var string
     */
    protected $ext = 'cache';

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        parent::__construct($options + array(
            'dir' => $this->dir
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function get($key, $expire = null, $fn = null)
    {
        if (!is_file($file = $this->getFile($key))) {
            $result = false;
        } else {
            $content = $this->getContent($file);;
            if ($content && is_array($content) && time() < $content[0]) {
                $result = $content[1];
            } else {
                $this->remove($key);
                $result = false;
            }
        }
        return $this->processGetResult($key, $result, $expire, $fn);
    }

    /**
     * {@inheritdoc}
     */
    public function set($key, $value, $expire = 0)
    {
        $file = $this->getFile($key);
        $content = $this->prepareContent($value, $expire);
        return (bool) file_put_contents($file, $content, LOCK_EX);
    }

    /**
     * {@inheritdoc}
     */
    public function remove($key)
    {
        if (is_file($file = $this->getFile($key))) {
            return unlink($file);
        } else {
            return false;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function exists($key)
    {
        if (!is_file($file = $this->getFile($key))) {
            return false;
        }

        $content = $this->getContent($file);
        if ($content && is_array($content) && time() < $content[0]) {
            return true;
        } else {
            $this->remove($key);
            return false;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function add($key, $value, $expire = 0)
    {
        $file = $this->getFile($key);

        if (!is_file($file)) {
            // Open and try to lock file immediately
            if (!$handle = $this->openAndLock($file, 'wb', LOCK_EX | LOCK_NB)) {
                return false;
            }

            $rewrite = false;
        } else {
            // Open file for reading and rewriting
            if (!$handle = $this->openAndLock($file, 'r+b', LOCK_EX)) {
                return false;
            }

            // The cache is not expired
            if ($this->readAndVerify($handle, $file)) {
                fclose($handle);
                return false;
            }

            $rewrite = true;
        }

        $content = $this->prepareContent($value, $expire);
        return $this->writeAndRelease($handle, $content, $rewrite);
    }

    /**
     * {@inheritdoc}
     */
    public function replace($key, $value, $expire = 0)
    {
        if (!is_file($file = $this->getFile($key))) {
            return false;
        }

        // Open file for reading and rewriting
        if (!$handle = $this->openAndLock($file, 'r+b', LOCK_EX)) {
            return false;
        }

        if (!$this->readAndVerify($handle, $file)) {
            fclose($handle);
            return false;
        }

        $content = $this->prepareContent($value, $expire);
        return $this->writeAndRelease($handle, $content, true);
    }

    /**
     * {@inheritdoc}
     */
    public function incr($key, $offset = 1)
    {
        $file = $this->getFile($key);

        if (!is_file($file)) {
            return $this->set($key, $offset) ? $offset : false;
        }

        // Open file for reading and rewriting
        if (!$handle = $this->openAndLock($file, 'r+b', LOCK_EX)) {
            return false;
        }

        // Prepare file content
        if (!$content = $this->readAndVerify($handle, $file)) {
            $content = $this->prepareContent($offset, 0);
            $result = $offset;
        } else {
            $result = $content[1] = (int) $content[1] + $offset;
{'fraction_non_alphanumeric': 0.16392370572207085, 'fraction_numerical': 0.0038147138964577656, 'mean_word_length': 1.623963397197598, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1953065', 'n_tokens_mistral': 2817, 'n_tokens_neox': 2738, 'n_words': 617}