src/Datastructure/Cache/LRUCache.php
public function put($key, $value): bool {
        if ($this->hashMap->containsKey($key)) {
            $node = $this->getNodeFromHead($key);
            $node->setKey($key);
            $node->setValue($value);
            $this->unset($node);
            $this->updateHead($node);
            return true;
        }
        if ($this->hashMap->size() >= $this->capacity) {
            $end = $this->getEnd();
            if (null !== $end) {
                $this->unset($end);
                $this->hashMap->remove($end->getKey());
            }
        }
        $node = new Node();
        $node->setKey($key);
        $node->setValue($value);
        $this->updateHead($node);
        $this->hashMap->add($key, $value);
        return true;
    }
public function put($key, $value): bool {
        if ($this->hashMap->containsKey($key)) {
            $node = $this->getNodeFromHead($key);
            $node->setKey($key);
            $node->setValue($value);
            $this->unset($node);
            $this->updateHead($node);
            return true;
        }
        if ($this->hashMap->size() >= $this->capacity) {
            $end = $this->getEnd();
            if (null !== $end) {
                $this->unset($end);
                $this->hashMap->remove($end->getKey());
            }
        }
        $node = new Node();
        $node->setKey($key);
        $node->setValue($value);
        $this->updateHead($node);
        $this->hashMap->add($key, $value);
        return true;
    }
public function put ( $ key , $ value ) : bool { if ( $ this -> hashMap -> containsKey ( $ key ) ) { $ node = $ this -> getNodeFromHead ( $ key ) ; $ node -> setKey ( $ key ) ; $ node -> setValue ( $ value ) ; $ this -> unset ( $ node ) ; $ this -> updateHead ( $ node ) ; return true ; } if ( $ this -> hashMap -> size ( ) >= $ this -> capacity ) { $ end = $ this -> getEnd ( ) ; if ( null !== $ end ) { $ this -> unset ( $ end ) ; $ this -> hashMap -> remove ( $ end -> getKey ( ) ) ; } } $ node = new Node ( ) ; $ node -> setKey ( $ key ) ; $ node -> setValue ( $ value ) ; $ this -> updateHead ( $ node ) ; $ this -> hashMap -> add ( $ key , $ value ) ; return true ; }
adds a new key value pair

@param $key
@param $value
@return bool
@throws \doganoo\PHPAlgorithms\common\Exception\InvalidKeyTypeException
@throws \doganoo\PHPAlgorithms\common\Exception\UnsupportedKeyTypeException
adds a new key value pair