<commit_before><?php

namespace EdpGithub\Listener;

use Zend\EventManager\ListenerAggregateInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\Event;

use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;

class Cache implements ListenerAggregateInterface, ServiceManagerAwareInterface
{
    /**
     * @var array
     */
    private $options;

    protected $listeners = array();

    protected $serviceManager;

    protected $cacheKey;

    protected $cache;

    public function attach(EventManagerInterface $events)
    {
        $this->listeners[] = $events->attach('pre.send', array($this, 'preSend'), -1000);
        $this->listeners[] = $events->attach('post.send', array($this, 'postSend'));
    }

    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$index]);
            }
        }
    }

    public function preSend(Event $e)
    {
        $request = $e->getTarget();

        $cache = $this->getCache();

        $this->cacheKey = md5($request);

        $response = $cache->getItem($this->cacheKey, $success);
        if ($success) {
            $tags = $cache->getTags($this->cacheKey);
            $request->getHeaders()->addHeaders(array(
                'If-None-Match' => $tags[0],
            ));
        }
    }

    public function postSend(Event $e)
    {
        $response = $e->getTarget();

        $statusCode =  $response->getStatusCode();

        $cache = $this->getCache();

        if ($statusCode == 304) {
            $response =  $cache->getItem($this->cacheKey);
        } else {
            $cache->setItem($this->cacheKey, $response);
            $headers = $response->getHeaders();
            $etag = $headers->get('Etag')->getFieldValue();
            $tags = array(
                'etag' => $etag,
            );
            $cache->setTags($this->cacheKey, $tags);
        }
        $e->stopPropagation(true);

        return $response;
    }

    public function getCache()
    {
        if ($this->cache === null) {
            /* @var $config array */
            $config = $this->getServiceManager()->get('Config');<commit_msg>Only set cache tags if ETag header is present.<commit_after><?php

namespace EdpGithub\Listener;

use Zend\EventManager\ListenerAggregateInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\Event;

use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;

class Cache implements ListenerAggregateInterface, ServiceManagerAwareInterface
{
    /**
     * @var array
     */
    private $options;

    protected $listeners = array();

    protected $serviceManager;

    protected $cacheKey;

    protected $cache;

    public function attach(EventManagerInterface $events)
    {
        $this->listeners[] = $events->attach('pre.send', array($this, 'preSend'), -1000);
        $this->listeners[] = $events->attach('post.send', array($this, 'postSend'));
    }

    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$index]);
            }
        }
    }

    public function preSend(Event $e)
    {
        $request = $e->getTarget();

        $cache = $this->getCache();

        $this->cacheKey = md5($request);

        $response = $cache->getItem($this->cacheKey, $success);
        if ($success) {
            $tags = $cache->getTags($this->cacheKey);
            if (isset($tags[0])) {
                $request->getHeaders()->addHeaders(array(
                    'If-None-Match' => $tags[0],
                ));
            }
        }
    }

    public function postSend(Event $e)
    {
        $response = $e->getTarget();

        $statusCode =  $response->getStatusCode();

        $cache = $this->getCache();

        if ($statusCode == 304) {
            $response =  $cache->getItem($this->cacheKey);
        } else {
            $cache->setItem($this->cacheKey, $response);
            $headers = $response->getHeaders();
            if ($headers->get('Etag')) {
                $etag = $headers->get('Etag')->getFieldValue();
                $tags = array(
                    'etag' => $etag,
                );
                $cache->setTags($this->cacheKey, $tags);
            }
        }
        $e->stopPropagation(true);

        return $response;
    }

    public function getCache()
    {
        if ($this->cache === null) {
            /* @var $config array */
            $config = $this->getServiceManager()->get('Config');
{'fraction_non_alphanumeric': 0.15279815552295117, 'fraction_numerical': 0.0039823936281701946, 'mean_word_length': 2.191973244147157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, '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': '6253206', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1324, 'n_words': 247}