public function put($key, $data) {
        if ($this->capacity <= 0) { return false; }
        if (isset($this->hashmap[$key]) && !empty($this->hashmap[$key])) {
            $node = $this->hashmap[$key];
            // update data
            $this->detach($node);
            $this->attach($this->head, $node);
            $node->setData($data);
        }
        else {
            $node = new Node($key, $data);
            $this->hashmap[$key] = $node;
            $this->attach($this->head, $node);

            // check if cache is full
            if (count($this->hashmap) > $this->capacity) {
                // we're full, remove the tail
                $nodeToRemove = $this->tail->getPrevious();
                $this->detach($nodeToRemove);
                unset($this->hashmap[$nodeToRemove->getKey()]);
            }
        }
        return true;
    }
public function put($key, $data) {
        if ($this->capacity <= 0) { return false; }
        if (isset($this->hashmap[$key]) && !empty($this->hashmap[$key])) {
            $node = $this->hashmap[$key];
            // update data
            $this->detach($node);
            $this->attach($this->head, $node);
            $node->setData($data);
        }
        else {
            $node = new Node($key, $data);
            $this->hashmap[$key] = $node;
            $this->attach($this->head, $node);

            // check if cache is full
            if (count($this->hashmap) > $this->capacity) {
                // we're full, remove the tail
                $nodeToRemove = $this->tail->getPrevious();
                $this->detach($nodeToRemove);
                unset($this->hashmap[$nodeToRemove->getKey()]);
            }
        }
        return true;
    }
public function put ( $ key , $ data ) { if ( $ this -> capacity <= 0 ) { return false ; } if ( isset ( $ this -> hashmap [ $ key ] ) && ! empty ( $ this -> hashmap [ $ key ] ) ) { $ node = $ this -> hashmap [ $ key ] ; // update data $ this -> detach ( $ node ) ; $ this -> attach ( $ this -> head , $ node ) ; $ node -> setData ( $ data ) ; } else { $ node = new Node ( $ key , $ data ) ; $ this -> hashmap [ $ key ] = $ node ; $ this -> attach ( $ this -> head , $ node ) ; // check if cache is full if ( count ( $ this -> hashmap ) > $ this -> capacity ) { // we're full, remove the tail $ nodeToRemove = $ this -> tail -> getPrevious ( ) ; $ this -> detach ( $ nodeToRemove ) ; unset ( $ this -> hashmap [ $ nodeToRemove -> getKey ( ) ] ) ; } } return true ; }
Inserts a new element into the cache
@param string $key the key of the new element
@param string $data the content of the new element
@return boolean true on success, false if cache has zero capacity
Inserts a new element into the cache