<commit_before><?php
	declare(strict_types = 1);

	namespace Edde\Ext\Cache;

	use Edde\Api\Cache\LazyCacheDirectoryTrait;
	use Edde\Api\File\IDirectory;
	use Edde\Common\Cache\AbstractCacheStorage;

	/**
	 * Cache is stored in one file based on a storage namespace.
	 */
	class FlatCacheStorage extends AbstractCacheStorage {
		use LazyCacheDirectoryTrait;
		/**
		 * @var string
		 */
		protected $namespace;
		/**
		 * @var IDirectory
		 */
		protected $directory;

		/**
		 * Two flies are sitting on a pile of dog poop. One suggests to the other: “Do you want to hear a really good joke?”
		 *
		 * The other fly replies: “But nothing disgusting like last time, I’m trying to eat here!”
		 *
		 * @param string $namespace
		 */
		public function __construct(string $namespace = '') {
			$this->namespace = $namespace;
		}

		protected function get() {
			return @unserialize(($content = file_get_contents($this->directory->filename('0.cache'))) ? $content : '');
		}

		public function save(string $id, $save) {
			$this->use();
			$this->write++;
			$source = $this->get();
			$source[$id] = $save;
			file_put_contents($this->directory->filename('0.cache'), serialize($source));
			return $save;
		}

		public function load($id) {
			$this->use();
			$source = $this->get();
			if (isset($source[$id])) {
				$this->hit++;
				return $source[$id];
			}
			$this->miss++;
			return null;
		}

		public function invalidate() {
			$this->use();
			$this->directory->purge();
		}

		protected function prepare() {
			parent::prepare();
			$this->cacheDirectory->create();
			$this->directory = $this->cacheDirectory->directory(sha1($this->namespace))
				->create();
		}
	}<commit_msg>- huge optimization of flat file cache storage - it will override data in concurrent requests which will sit down by time<commit_after><?php
	declare(strict_types = 1);

	namespace Edde\Ext\Cache;

	use Edde\Api\Cache\LazyCacheDirectoryTrait;
	use Edde\Api\File\IDirectory;
	use Edde\Common\Cache\AbstractCacheStorage;

	/**
	 * Cache is stored in one file based on a storage namespace.
	 */
	class FlatCacheStorage extends AbstractCacheStorage {
		use LazyCacheDirectoryTrait;
		/**
		 * @var string
		 */
		protected $namespace;
		/**
		 * @var IDirectory
		 */
		protected $directory;
		/**
		 * @var array
		 */
		protected $source = [];

		/**
		 * Two flies are sitting on a pile of dog poop. One suggests to the other: “Do you want to hear a really good joke?”
		 *
		 * The other fly replies: “But nothing disgusting like last time, I’m trying to eat here!”
		 *
		 * @param string $namespace
		 */
		public function __construct(string $namespace = '') {
			$this->namespace = $namespace;
		}

		public function save(string $id, $save) {
			$this->use();
			$this->write++;
			$this->source[$id] = $save;
			file_put_contents($this->directory->filename('0.cache'), serialize($this->source));
			return $save;
		}

		public function load($id) {
			$this->use();
			/** @noinspection NotOptimalIfConditionsInspection */
			if (isset($this->source[$id]) || array_key_exists($id, $this->source)) {
				$this->hit++;
				return $this->source[$id];
			}
			$this->miss++;
			return null;
		}

		public function invalidate() {
			$this->use();
			$this->directory->purge();
		}

		protected function prepare() {
			parent::prepare();
			$this->cacheDirectory->create();
			$this->directory = $this->cacheDirectory->directory(sha1($this->namespace))
				->create();
			$this->source = @unserialize(($content = file_get_contents($this->directory->filename('0.cache'))) ? $content : '');
		}
	}
{'fraction_non_alphanumeric': 0.15893854748603353, 'fraction_numerical': 0.0022346368715083797, 'mean_word_length': 3.5100755667506296, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, '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': '4997857', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1091, 'n_words': 287}