Last active
January 6, 2017 09:51
-
-
Save replsv/9aaf3524d59fc867a44bbf5ca8aeecc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
composer.json | |
{ | |
"name": "predis-copy-keys", | |
"require": { | |
"predis/predis": "^1.1" | |
}, | |
"authors": [ | |
{ | |
"name": "Gabriel C.", | |
"email": "eu@lazycoder.ro" | |
} | |
] | |
} | |
/* | |
require 'vendor/autoload.php'; | |
$redis = new Predis\Client('tcp://redis'); | |
$source = 'SOURCE'; | |
$destination = 'DESTINATION'; | |
$keys = [2, 3, 4, 5, 6, 7, 8]; // keys to copy - applies for hashes | |
$script = <<<LUA | |
local hash = redis.call('hgetall', KEYS[1]); | |
if #hash == 0 then | |
return { err = 'Unknown key "'..KEYS[1]..'" 1' } | |
end | |
return redis.call('hmset', KEYS[2], unpack(hash)); | |
LUA; | |
foreach ($keys as $memberToCopy) { | |
$redis->eval($script, 2, $source, $destination); | |
} | |
$copiedKey = $redis->hgetall($destination); | |
echo sprintf('-- Copied %s over %s\n\n\n', $source, $destination); | |
var_dump(array_keys($copiedKey)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment