<commit_before><?php
/**
 * Copyright (C) 2014 <NAME>
 *
 * Defines the cache for rendered templates
 */
namespace RDev\Views\Templates;
use RDev\Models\Files;

class Cache implements ICache
{
    /** The default lifetime of a cached template */
    const DEFAULT_LIFETIME = 3600;

    /** @var Files\FileSystem The file system to use to read cached templates */
    private $fileSystem = null;
    /** @var string The path to store the cached templates at */
    private $path = null;
    /** @var int The number of seconds cached templates should live */
    private $lifetime = self::DEFAULT_LIFETIME;

    /**
     * @param Files\FileSystem $fileSystem The file system to use to read cached template
     * @param string $path The path to store the cached templates at
     * @param int $lifetime The number of seconds cached templates should live
     */
    public function __construct(Files\FileSystem $fileSystem, $path, $lifetime = self::DEFAULT_LIFETIME)
    {
        $this->fileSystem = $fileSystem;
        $this->path = rtrim($path, "/");
        $this->setLifetime($lifetime);

        // Every so often, run some garbage collection
        if(rand(1, 100) == 50)
        {
            $this->gc();
        }
    }

    /**
     * {@inheritdoc}
     */
    public function flush()
    {
        $templatePaths = $this->fileSystem->getFiles($this->path);

        foreach($templatePaths as $templatePath)
        {
            $this->fileSystem->deleteFile($templatePath);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function gc()
    {
        $templatePaths = $this->fileSystem->getFiles($this->path);

        foreach($templatePaths as $templatePath)
        {
            if($this->isExpired($templatePath))
            {
                $this->fileSystem->deleteFile($templatePath);
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public function get($unrenderedTemplate, array $variables = [], array $tags = [])
    {
        if(!$this->has($unrenderedTemplate, $variables, $tags))
        {
            return null;
        }

        return $this->fileSystem->read($this->getTemplatePath($unrenderedTemplate, $variables, $tags));
    }

    /**
     * {@inheritdoc}
     */
    public function getLifetime()
    {
        return $this->lifetime;
    }

    /**
     * {@inheritdoc}
     */
    public function has($unrenderedTemplate, array $variables = [], array $tags = [])
    {
        if(!$this->cachingIsEnabled())
        {
            return false;
        }

        $templatePath = $this->getTemplatePath($unrenderedTemplate, $variables, $tags);
        $exists = $this->fileSystem->exists($templatePath);

        if(!$exists)
        {
            return false;
        }

        // Check the expiration
        if($this->isExpired($templatePath))
        {
            // Do some garbage collection
            $this->fileSystem->deleteFile($templatePath);

            return false;
        }

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function set($renderedTemplate, $unrenderedTemplate, array $variables = [], array $tags = [])
    {
        if($this->cachingIsEnabled())
        {
            $this->fileSystem->write($this->getTemplatePath($unrenderedTemplate, $variables, $tags), $renderedTemplate);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function setLifetime($lifetime)
    {
        $this->lifetime = $lifetime;
    }

    /**
     * Gets whether or not caching is enabled
     *
     * @return bool True if caching is enabled, otherwise false
     */
    private function cachingIsEnabled()
    {
        return $this->lifetime > 0;
    }

    /**
     * Gets path to cached template
     *
     * @param string $unrenderedTemplate The unrendered template
     * @param array $variables The list of variables used by this template
     * @param array $tags The list of tag values used by this template
     * @return string The path to the cached template
     */
    private function getTemplatePath($unrenderedTemplate, array $variables, array $tags)
    {
        return $this->path . "/" . md5(http_build_query([
            "te" => $unrenderedTemplate,
            "v" => $variables,
            "ta" => $tags
        ]));
    }

    /**
     * Checks whether or not a template path is expired
     *
     * @param string $templatePath The template path to check
     * @return bool True if the path is expired, otherwise false
     */
    private function isExpired($templatePath)
    {
        return $this->fileSystem->getLastModified($templatePath) < new \DateTime("-" . $this->lifetime . " seconds");
    }
}<commit_msg>Changed they cache key format<commit_after><?php
/**
 * Copyright (C) 2014 <NAME>
 *
 * Defines the cache for rendered templates
 */
namespace RDev\Views\Templates;
use RDev\Models\Files;

class Cache implements ICache
{
    /** The default lifetime of a cached template */
    const DEFAULT_LIFETIME = 3600;

    /** @var Files\FileSystem The file system to use to read cached templates */
    private $fileSystem = null;
    /** @var string The path to store the cached templates at */
    private $path = null;
    /** @var int The number of seconds cached templates should live */
    private $lifetime = self::DEFAULT_LIFETIME;

    /**
     * @param Files\FileSystem $fileSystem The file system to use to read cached template
     * @param string $path The path to store the cached templates at
     * @param int $lifetime The number of seconds cached templates should live
     */
    public function __construct(Files\FileSystem $fileSystem, $path, $lifetime = self::DEFAULT_LIFETIME)
    {
        $this->fileSystem = $fileSystem;
        $this->path = rtrim($path, "/");
        $this->setLifetime($lifetime);

        // Every so often, run some garbage collection
        if(rand(1, 100) == 50)
        {
            $this->gc();
        }
    }

    /**
     * {@inheritdoc}
     */
    public function flush()
    {
        $templatePaths = $this->fileSystem->getFiles($this->path);

        foreach($templatePaths as $templatePath)
        {
            $this->fileSystem->deleteFile($templatePath);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function gc()
    {
        $templatePaths = $this->fileSystem->getFiles($this->path);

        foreach($templatePaths as $templatePath)
        {
            if($this->isExpired($templatePath))
            {
                $this->fileSystem->deleteFile($templatePath);
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public function get($unrenderedTemplate, array $variables = [], array $tags = [])
    {
        if(!$this->has($unrenderedTemplate, $variables, $tags))
        {
            return null;
        }

        return $this->fileSystem->read($this->getTemplatePath($unrenderedTemplate, $variables, $tags));
    }

    /**
     * {@inheritdoc}
     */
    public function getLifetime()
    {
        return $this->lifetime;
    }

    /**
     * {@inheritdoc}
     */
    public function has($unrenderedTemplate, array $variables = [], array $tags = [])
    {
        if(!$this->cachingIsEnabled())
        {
            return false;
        }

        $templatePath = $this->getTemplatePath($unrenderedTemplate, $variables, $tags);
        $exists = $this->fileSystem->exists($templatePath);

        if(!$exists)
        {
            return false;
        }

        // Check the expiration
        if($this->isExpired($templatePath))
        {
            // Do some garbage collection
            $this->fileSystem->deleteFile($templatePath);

            return false;
        }

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function set($renderedTemplate, $unrenderedTemplate, array $variables = [], array $tags = [])
    {
        if($this->cachingIsEnabled())
        {
            $this->fileSystem->write($this->getTemplatePath($unrenderedTemplate, $variables, $tags), $renderedTemplate);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function setLifetime($lifetime)
    {
        $this->lifetime = $lifetime;
    }

    /**
     * Gets whether or not caching is enabled
     *
     * @return bool True if caching is enabled, otherwise false
     */
    private function cachingIsEnabled()
    {
        return $this->lifetime > 0;
    }

    /**
     * Gets path to cached template
     *
     * @param string $unrenderedTemplate The unrendered template
     * @param array $variables The list of variables used by this template
     * @param array $tags The list of tag values used by this template
     * @return string The path to the cached template
     */
    private function getTemplatePath($unrenderedTemplate, array $variables, array $tags)
    {
        return $this->path . "/" . md5(http_build_query([
            "u" => $unrenderedTemplate,
            "v" => $variables,
            "t" => $tags
        ]));
    }

    /**
     * Checks whether or not a template path is expired
     *
     * @param string $templatePath The template path to check
     * @return bool True if the path is expired, otherwise false
     */
    private function isExpired($templatePath)
    {
        return $this->fileSystem->getLastModified($templatePath) < new \DateTime("-" . $this->lifetime . " seconds");
    }
}
{'fraction_non_alphanumeric': 0.12481331342009815, 'fraction_numerical': 0.0034136974610625135, 'mean_word_length': 2.126042014004668, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 87, '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': '357673', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2535, 'n_words': 682}