MatthiasMullie\Scrapbook\Scale\Shard::getShard PHP Method

getShard() protected method

Get the shard (KeyValueStore object) that corresponds to a particular cache key.
protected getShard ( string $key ) : MatthiasMullie\Scrapbook\KeyValueStore
$key string
return MatthiasMullie\Scrapbook\KeyValueStore
    protected function getShard($key)
    {
        /*
         * The hash is so we can deterministically randomize the spread of keys
         * over servers: if we were to just spread them based on key name, we
         * may end up with a large chunk of similarly prefixed keys on the same
         * server. Hashing the key will ensure similar looking keys can still
         * result in very different values, yet they result will be the same
         * every time it's repeated for the same key.
         * Since we don't use the hash for encryption, the fastest algorithm
         * will do just fine here.
         */
        $hash = crc32($key);
        // crc32 on 32-bit machines can produce a negative int
        $hash = abs($hash);
        $index = $hash % count($this->caches);
        return $this->caches[$index];
    }