PHPDaemon\Cache\CappedStorage::put PHP Method

put() public method

Puts element in cache
public put ( string $key, mixed $value, integer $ttl = null ) : mixed
$key string Key
$value mixed Value
$ttl integer Time to live
return mixed
    public function put($key, $value, $ttl = null)
    {
        $k = $this->hash($key);
        if (isset($this->cache[$k])) {
            $item = $this->cache[$k];
            $item->setValue($value);
            if ($ttl !== null) {
                $item->expire = microtime(true) + $ttl;
            }
            return $item;
        }
        $item = new Item($value);
        if ($ttl !== null) {
            $item->expire = microtime(true) + $ttl;
        }
        $this->cache[$k] = $item;
        $s = sizeof($this->cache);
        if ($s > $this->maxCacheSize + $this->capWindow) {
            uasort($this->cache, $this->sorter);
            for (; $s > $this->maxCacheSize; --$s) {
                array_pop($this->cache);
            }
        }
        return $item;
    }