Skip to content


Repository files navigation


Easily sync an iterable ruby object with a redis namespace.

This tool DOES NOT:

  • integrate with anything
  • help retrieve data back from redis
  • handle redis connection issues
  • make birds in 5 mile radius attack you


Add this line to your application's Gemfile:

gem 'redis_collection'

And then execute:

$ bundle

Or install it yourself as:

$ gem install redis_collection


books = [
  { id: 1, title: 'Programming Elixir' },
  { id: 2, title: 'Programming Phoenix' }

redis = ENV['REDIS_URL'])
redis_collection =, namespace: 'books_')
redis.mget('books_1', 'books_2')
# => ["\x04\b{\aI\"\aid\x06:\x06ETi\x06I\"\ntitle\x06;\x00TI\"\x17Programming Elixir\x06;\x00T", "\x04\b{\aI\"\aid\x06:\x06ETi\aI\"\ntitle\x06;\x00TI\"\x18Programming Phoenix\x06;\x00T"]

By default each object in the collection is serialized via Marshal.dump and loaded via Marshal.load. Identity of each object is determined by calling ['id'] by default. To change these defaults you can provide procs to

redis_collection =,
  namespace: 'books_',
  load: -> string { JSON.parse(string) },
  dump: -> object { object.to_json },
  make_key: -> object { object.special_id }

In the above example objects are converted to/from JSON and must respond to special_id to be identified.

Method sync always overwrites keys that already exist in Redis, and deletes keys that are in Redis but not in collection.


Method sync returns a hash of stats containing 3 pieces of information:

  1. :mset_cnt - how many strings were mset
  2. :del_cnt - how many strings were del
  3. :time - the result of Benchmark.measure {} on the redis call


After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.


Sync a ruby enumerable with a redis namespace



Code of conduct