<commit_before><?php
/**
 * @file
 */

namespace CultuurNet\UDB3\UiTID;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use ValueObjects\String\String;
use ValueObjects\Web\EmailAddress;

class InMemoryCacheDecoratedUsers implements UsersInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * @var UsersInterface
     */
    private $wrapped;

    /**
     * @var String[]
     */
    private $nickMap;

    /**
     * @var String[]
     */
    private $mailMap;

    /**
     * @param UsersInterface $wrapped
     */
    public function __construct(UsersInterface $wrapped)
    {
        $this->wrapped = $wrapped;
        $this->logger = new NullLogger();
    }

    /**
     * @inheritdoc
     */
    public function byEmail(EmailAddress $email)
    {
        $key = $email->toNative();
        if (!isset($this->mailMap[$key])) {
            $this->mailMap[$key] = $this->wrapped->byEmail($email);
        } else {
            $this->logger->info('found user id of ' . $email->toNative() . ' in cache');
        }

        return $this->mailMap[$key];
    }

    /**
     * @inheritdoc
     */
    public function byNick(String $nick)
    {
        $key = $nick->toNative();
        if (!isset($this->nickMap[$key])) {
            $this->nickMap[$key] = $this->wrapped->byNick($nick);
        } else {
            $this->logger->info('found user id of ' . $nick->toNative() . ' in cache');
        }

        return $this->nickMap[$key];
    }
}<commit_msg>III-810: Also use cache for items that couldn't be found on previous attempts, to improve speed<commit_after><?php
/**
 * @file
 */

namespace CultuurNet\UDB3\UiTID;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use ValueObjects\String\String;
use ValueObjects\Web\EmailAddress;

class InMemoryCacheDecoratedUsers implements UsersInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * @var UsersInterface
     */
    private $wrapped;

    /**
     * @var String[]
     */
    private $nickMap;

    /**
     * @var String[]
     */
    private $mailMap;

    /**
     * @param UsersInterface $wrapped
     */
    public function __construct(UsersInterface $wrapped)
    {
        $this->wrapped = $wrapped;
        $this->logger = new NullLogger();
        $this->nickMap = [];
        $this->mailMap = [];
    }

    /**
     * @inheritdoc
     */
    public function byEmail(EmailAddress $email)
    {
        $key = $email->toNative();
        if (!array_key_exists($key, $this->mailMap)) {
            $this->mailMap[$key] = $this->wrapped->byEmail($email);
        } else {
            $this->logger->info(
                'found user id mapping of ' . $email->toNative() . ' in cache'
            );
        }

        return $this->mailMap[$key];
    }

    /**
     * @inheritdoc
     */
    public function byNick(String $nick)
    {
        $key = $nick->toNative();
        if (!array_key_exists($key, $this->nickMap)) {
            $this->nickMap[$key] = $this->wrapped->byNick($nick);
        } else {
            $this->logger->info(
                'found user id mapping of ' . $nick->toNative() . ' in cache'
            );
        }

        return $this->nickMap[$key];
    }
}
{'fraction_non_alphanumeric': 0.1540803897685749, 'fraction_numerical': 0.0015225334957369061, 'mean_word_length': 2.084507042253521, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '7308449', 'n_tokens_mistral': 1048, 'n_tokens_neox': 1023, 'n_words': 197}