protected function check_ttl() {
        if ($this->ttl === 0) {
            return 0;
        }
        $maxtime = cache::now() - $this->ttl;
        $count = 0;
        for ($value = reset($this->store); $value !== false; $value = next($this->store)) {
            if ($value[1] >= $maxtime) {
                // We know that elements are sorted by ttl so no need to continue.
                break;
            }
            $count++;
        }
        if ($count) {
            // Remove first $count elements as they are expired.
            $this->store = array_slice($this->store, $count, null, true);
            if ($this->maxsize !== false) {
                $this->storecount -= $count;
            }
        }
        return $count;
    }