Skip to content

Instantly share code, notes, and snippets.

@elight
Created March 17, 2011 22:48
Show Gist options
  • Save elight/875305 to your computer and use it in GitHub Desktop.
Save elight/875305 to your computer and use it in GitHub Desktop.
Redis key sort chaining gets example
require 'rubygems'
require 'redis'
r = Redis.new
r.flushdb
42.times { |i| r.lpush "nums", i }
42.times { |i| r.set "weight_#{i}", i * 2 + 1 }
42.times { |i| r.set "weight2_#{i}", i * 4 + 3 }
42.times { |i| r.set "weight3_#{i}", i * 8 + 5 }
42.times { |i| r.set "weight4_#{i}", i * 16 + 7 }
p r.sort "nums", :by => "weight2_*", :get => ["weight3_*", "weight4_*"]
p r.sort "nums", :by => "weight2_*", :get => "weight3_*"
p r.sort "nums", :by => "weight2_*", :get => "weight4_*"
# outputs
# ["5", "7", "13", "23", "21", "39", "29", "55", "37", "71", "45", "87", "53", "103", "61", "119", "69", "135", "77", "151", "85", "167", "93", "183", "101", "199", "109", "215", "117", "231", "125", "247", "133", "263", "141", "279", "149", "295", "157", "311", "165", "327", "173", "343", "181", "359", "189", "375", "197", "391", "205", "407", "213", "423", "221", "439", "229", "455", "237", "471", "245", "487", "253", "503", "261", "519", "269", "535", "277", "551", "285", "567", "293", "583", "301", "599", "309", "615", "317", "631", "325", "647", "333", "663"]
# ["5", "13", "21", "29", "37", "45", "53", "61", "69", "77", "85", "93", "101", "109", "117", "125", "133", "141", "149", "157", "165", "173", "181", "189", "197", "205", "213", "221", "229", "237", "245", "253", "261", "269", "277", "285", "293", "301", "309", "317", "325", "333"]
# ["7", "23", "39", "55", "71", "87", "103", "119", "135", "151", "167", "183", "199", "215", "231", "247", "263", "279", "295", "311", "327", "343", "359", "375", "391", "407", "423", "439", "455", "471", "487", "503", "519", "535", "551", "567", "583", "599", "615", "631", "647", "663"]
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment