<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Parth Patel
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\library;

use seekquarry\yioop\library as L;

/**
 * Implements a least recently used cache
 *
 * @author Parth Patel
 */
class LRUCache
{
    /**
     * An associative array that represent cache
     * @var array
     */
    private $cache;
    /**
     * Default size of LRU Cache
     * @var int
     */
    const DEFAULT_SIZE = 200;
    /**
     * Creates an empty cache and sets the size
     *
     * @param int $size size of the cache
     */
    public function __construct(private $size = self::DEFAULT_SIZE)
    {
        $this->cache = [];
    }
    /**
     * Add or update a key with given value to the cache
     *
     * @param mixed $key used to look up value
     * @param mixed $value associated to the key
     * @return mixed evicted key-value pair if any
     */
    public function put($key, $value)
    {
        //use array_key_values rather than isset as value might be null
        if (array_key_exists($key, $this->cache)) {
            unset($this->cache[$key]);
            // move to front
            $this->cache = [$key => $value] + $this->cache;
        } else {
            if (count($this->cache) < $this->size) {
                // insert at front
                $this->cache = [$key => $value] + $this->cache;
            } else {
                $evicted_key = array_key_last($this->cache);
                $evicted_value = $this->cache[$evicted_key];
                unset($this->cache[$evicted_key]);
                $this->cache = [$key => $value] + $this->cache;
                return [$evicted_key, $evicted_value];
            }
        }
    }
    /**
     * Returns the value for a given key if found in the cache
     *
     * @param mixed $key used to look up value
     * @return mixed $value if found
     */
    public function get($key)
    {
        if (array_key_exists($key, $this->cache)) {
            return $this->cache[$key];
        }
    }
    /**
     * Returns all the items currently in cache
     *
     * @return array $this->cache
     */
    public function getAll()
    {
        return $this->cache;
    }
}
