<commit_before><?php

/**
 * This file is part of the Liquid package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package Liquid
 */

namespace Liquid\Cache;

use Liquid\Cache;
use Liquid\LiquidException;

/**
 * Implements cache stored in files.
 */
class File extends Cache
{
	/**
	 * Constructor.
	 *
	 * It checks the availability of cache directory.
	 *
	 * @param array $options
	 *
	 * @throws LiquidException if Cachedir not exists.
	 */
	public function __construct(array $options = array()) {
		parent::__construct($options);

		if (isset($options['cache_dir']) && is_writable($options['cache_dir'])) {
			$this->path = realpath($options['cache_dir']) . DIRECTORY_SEPARATOR;
		} else {
			throw new LiquidException('Cachedir not exists or not writable');
		}
	}

	/**
	 * {@inheritdoc}
	 */
	public function read($key, $unserialize = true) {
		if (!$this->exists($key))
			return false;

		if ($unserialize) {
			return unserialize(file_get_contents($this->path . $this->prefix . $key));
		}

		return file_get_contents($this->path . $this->prefix . $key);
	}

	/**
	 * {@inheritdoc}
	 */
	public function exists($key) {
		$cacheFile = $this->path . $this->prefix . $key;

		if (!file_exists($cacheFile) || filemtime($cacheFile) + $this->expire < time()) {
			return false;
		}

		return true;
	}

	/**
	 * {@inheritdoc}
	 */
	public function write($key, $value, $serialize = true) {
		if (file_put_contents($this->path . $this->prefix . $key, $serialize ? serialize($value) : $value) !== false) {
			$this->gc();
			return true;
		}

		throw new LiquidException('Can not write cache file');
	}

	/**
	 * {@inheritdoc}
	 */
	public function flush($expiredOnly = false) {
		foreach (glob($this->path . $this->prefix . '*') as $file) {
			if ($expiredOnly) {
				if (filemtime($file) + $this->expire < time()) {
					unlink($file);
				}
			} else {
				unlink($file);
			}
		}
	}

	/**
	 * {@inheritdoc}
	 */
	protected function gc() {
		$this->flush(true);
	}
}<commit_msg>There's no point throwing an exception in Cache/File because file_put_contents will trigger an error all the same<commit_after><?php

/**
 * This file is part of the Liquid package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package Liquid
 */

namespace Liquid\Cache;

use Liquid\Cache;
use Liquid\LiquidException;

/**
 * Implements cache stored in files.
 */
class File extends Cache
{
	/**
	 * Constructor.
	 *
	 * It checks the availability of cache directory.
	 *
	 * @param array $options
	 *
	 * @throws LiquidException if Cachedir not exists.
	 */
	public function __construct(array $options = array()) {
		parent::__construct($options);

		if (isset($options['cache_dir']) && is_writable($options['cache_dir'])) {
			$this->path = realpath($options['cache_dir']) . DIRECTORY_SEPARATOR;
		} else {
			throw new LiquidException('Cachedir not exists or not writable');
		}
	}

	/**
	 * {@inheritdoc}
	 */
	public function read($key, $unserialize = true) {
		if (!$this->exists($key))
			return false;

		if ($unserialize) {
			return unserialize(file_get_contents($this->path . $this->prefix . $key));
		}

		return file_get_contents($this->path . $this->prefix . $key);
	}

	/**
	 * {@inheritdoc}
	 */
	public function exists($key) {
		$cacheFile = $this->path . $this->prefix . $key;

		if (!file_exists($cacheFile) || filemtime($cacheFile) + $this->expire < time()) {
			return false;
		}

		return true;
	}

	/**
	 * {@inheritdoc}
	 */
	public function write($key, $value, $serialize = true) {
		$bytes = file_put_contents($this->path . $this->prefix . $key, $serialize ? serialize($value) : $value);
		$this->gc();

		return $bytes !== false;
	}

	/**
	 * {@inheritdoc}
	 */
	public function flush($expiredOnly = false) {
		foreach (glob($this->path . $this->prefix . '*') as $file) {
			if ($expiredOnly) {
				if (filemtime($file) + $this->expire < time()) {
					unlink($file);
				}
			} else {
				unlink($file);
			}
		}
	}

	/**
	 * {@inheritdoc}
	 */
	protected function gc() {
		$this->flush(true);
	}
}
{'fraction_non_alphanumeric': 0.16666666666666666, 'fraction_numerical': 0.0, 'mean_word_length': 3.39727463312369, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, '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': '891602', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1348, 'n_words': 347}