<commit_before><?php

namespace Kibo\Phast\Retrievers;

use Kibo\Phast\Cache\Cache;
use Kibo\Phast\ValueObjects\URL;

class CachingRetriever implements Retriever {

    /**
     * @var Cache
     */
    private $cache;

    /**
     * @var Retriever
     */
    private $retriever;

    /**
     * @var int
     */
    private $defaultCacheTime;

    /**
     * CachingRetriever constructor.
     *
     * @param Retriever $retriever
     * @param Cache $cache
     * @param int $defaultCacheTime
     */
    public function __construct(Cache $cache, Retriever $retriever = null, $defaultCacheTime = 0) {
        $this->retriever = $retriever;
        $this->cache = $cache;
        $this->defaultCacheTime = $defaultCacheTime;
    }

    public function retrieve(URL $url) {
        if ($this->retriever) {
            return $this->getCachedWithRetriever($url);
        }
        return $this->getFromCacheOnly($url);
    }

    public function getCacheSalt(URL $url) {
        if ($this->retriever) {
            return $this->retriever->getCacheSalt($url);
        }
        // TODO: Have this configurable
        // TODO: Consider not  copy/pasting from RemoteRetriever
        return md5($url->toString()) . '-' . floor(time() / 7200);
    }

    private function getCachedWithRetriever(URL $url) {
        return $this->cache->get((string)$url, function () use ($url) {
            return $this->retriever->retrieve($url);
        }, $this->defaultCacheTime);
    }

    private function getFromCacheOnly(URL $url) {
        $cached = $this->cache->get((string)$url);
        if (!$cached) {
            return false;
        }
        return $cached;
    }

}<commit_msg>Return empty string from caching retriever if no retriever is set<commit_after><?php

namespace Kibo\Phast\Retrievers;

use Kibo\Phast\Cache\Cache;
use Kibo\Phast\ValueObjects\URL;

class CachingRetriever implements Retriever {

    /**
     * @var Cache
     */
    private $cache;

    /**
     * @var Retriever
     */
    private $retriever;

    /**
     * @var int
     */
    private $defaultCacheTime;

    /**
     * CachingRetriever constructor.
     *
     * @param Retriever $retriever
     * @param Cache $cache
     * @param int $defaultCacheTime
     */
    public function __construct(Cache $cache, Retriever $retriever = null, $defaultCacheTime = 0) {
        $this->retriever = $retriever;
        $this->cache = $cache;
        $this->defaultCacheTime = $defaultCacheTime;
    }

    public function retrieve(URL $url) {
        if ($this->retriever) {
            return $this->getCachedWithRetriever($url);
        }
        return $this->getFromCacheOnly($url);
    }

    public function getCacheSalt(URL $url) {
        if ($this->retriever) {
            return $this->retriever->getCacheSalt($url);
        }
        return '';
    }

    private function getCachedWithRetriever(URL $url) {
        return $this->cache->get((string)$url, function () use ($url) {
            return $this->retriever->retrieve($url);
        }, $this->defaultCacheTime);
    }

    private function getFromCacheOnly(URL $url) {
        $cached = $this->cache->get((string)$url);
        if (!$cached) {
            return false;
        }
        return $cached;
    }

}
{'fraction_non_alphanumeric': 0.13255599877262964, 'fraction_numerical': 0.0021478981282602025, 'mean_word_length': 2.234126984126984, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, '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': '6066487', 'n_tokens_mistral': 1029, 'n_tokens_neox': 1001, 'n_words': 207}