Donnerstag, 6. Februar 2014

PHP Memcache list / get keys

/**
     * @param string $server
     * @param int $port
     * @param int $limit
     * @return array
     */
    public function getMemcacheKeys ($server, $port, $limit = 10000)
    {
        $keysFound = array();

        $options = $this->_options;
        $server = $options['servers'][0];
        $memcache = new Memcache;
        $memcache->connect($server, $port = 11211, 5);

        $slabs = $memcache->getExtendedStats('slabs');
        foreach ($slabs as $serverSlabs) {
            foreach ($serverSlabs as $slabId => $slabMeta) {
                try {
                    $cacheDump = $memcache->getExtendedStats('cachedump', (int) $slabId, 1000);
                } catch (Exception $e) {
                    continue;
                }

                if (!is_array($cacheDump)) {
                    continue;
                }

                foreach ($cacheDump as $dump) {
                    
                    if (!is_array($dump)) {
                        continue;
                    }

                    foreach ($dump as $key => $value) {
                        $keysFound[] = $key;

                        if (count($keysFound) == $limit) {
                            return $keysFound;
                        }
                    }
                }
            }
        }

        return $keysFound;
    }

Keine Kommentare:

Kommentar veröffentlichen