<commit_before><?php
namespace Repositories;

class CacheRepository extends Repository implements RepositoryInterface
{
    private $repo;
    private $source;

    public function __construct(\Illuminate\Cache\Repository $repo)
    {
        $this->repo = $repo;
    }

    public function setSource($source)
    {
        $this->source = $source;
    }

    public function read()
    {
        list($database, $index) = $this->source->read();

        foreach ($database as $repo_id => $object) {
            $this->repo->forever("cdda:db:$repo_id", $object);
        }

        foreach ($index as $id => $data) {
            $this->repo->forever("cdda:index:$id", $data);
        }
        $this->repo->forever("cdda:version", $this->source->version());
        return [$database, $index];
    }

    public function get($index, $id)
    {
        $indexDb = $this->all($index);
        if (!isset($indexDb[$id])) {
            return;
        }

        $repo_id = $indexDb[$id];

        if (!isset($this->database[$repo_id])) {
            $this->database[$repo_id] = $this->repo->get("cdda:db:$repo_id");
        }

        return $this->database[$repo_id];
    }

    public function all($index)
    {
        if (!isset($this->index[$index])) {
            $this->index[$index] = $this->repo->get("cdda:index:$index")?: array();
        }

        return $this->index[$index];
    }

    public function searchObjects($repo, $search)
    {
        $key = "search:$repo:".str_replace(" ", "!", $search);

        $expiration = \Config::get("cataclysm.searchCacheExpiration");

        $repoInstance = \App::make("Repositories\\Indexers\\$repo");
        $idField = $repoInstance::ID_FIELD;

        $objects = \Cache::remember($key, $expiration, 
            function () use ($repo, $search, $idField) {
                $objects = parent::searchObjects($repo, $search);
                array_walk($objects, function (&$object) use ($idField) {
                    $object = $object->$idField;
                });

            return $objects;
        });
<commit_msg>Inject the app and config instances to the CacheRepository.<commit_after><?php
namespace Repositories;

class CacheRepository extends Repository implements RepositoryInterface
{
    private $app;
    private $repo;
    private $source;
    private $expiration;

    public function __construct(
        \Illuminate\Cache\Repository $repo,
        \Illuminate\Foundation\Application $app,
        \Illuminate\Config\Repository $config
    )
    {
        $this->repo = $repo;
        $this->app = $app;
        $this->expiration = $config->get("cataclysm.searchCacheExpiration");
    }

    public function setSource($source)
    {
        $this->source = $source;
    }

    public function read()
    {
        list($database, $index) = $this->source->read();

        foreach ($database as $repo_id => $object) {
            $this->repo->forever("cdda:db:$repo_id", $object);
        }

        foreach ($index as $id => $data) {
            $this->repo->forever("cdda:index:$id", $data);
        }
        $this->repo->forever("cdda:version", $this->source->version());
        return [$database, $index];
    }

    public function get($index, $id)
    {
        $indexDb = $this->all($index);
        if (!isset($indexDb[$id])) {
            return;
        }

        $repo_id = $indexDb[$id];

        if (!isset($this->database[$repo_id])) {
            $this->database[$repo_id] = $this->repo->get("cdda:db:$repo_id");
        }

        return $this->database[$repo_id];
    }

    public function all($index)
    {
        if (!isset($this->index[$index])) {
            $this->index[$index] = $this->repo->get("cdda:index:$index")?: array();
        }

        return $this->index[$index];
    }

    public function searchObjects($repo, $search)
    {
        $key = "search:$repo:".str_replace(" ", "!", $search);

        $repoInstance = $this->app->make("Repositories\\Indexers\\$repo");
        $idField = $repoInstance::ID_FIELD;

        $objects = $this->repo->remember($key, $this->expiration, 
            function () use ($repo, $search, $idField) {
                $objects = parent::searchObjects($repo, $search);
                array_walk($objects, function (&$object) use ($idField) {
                    $object = $object->$idField;
                });

            return $objects;
        });

{'fraction_non_alphanumeric': 0.17204545454545456, 'fraction_numerical': 0.0, 'mean_word_length': 2.262416604892513, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '3010403', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1286, 'n_words': 237}