0.0
No commit activity in last 3 years
No release in over 3 years
A Ruby gem for cleaning up of Redis keys by pattern matching. Handles huge amounts of keys. Can be used with any Redis client that responds to #del and #keys
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
>= 0
 Project Readme

Redis-Cleaner Build Status Code Climate

A simple way of cleaning up a large number of Redis keys via pattern matching. Compatible with any Redis client for Ruby that responds in the same way as the official Redis client for Ruby does to the 2 methods: #del and #keys. These include but are not limited to:

Why not just use the CLI?

In most cases, you can probably get away with doing $redis-cli KEYS "pattern:*" | xargs redis-cli DEL !

But if you have a huge number of keys returned by the pattern (the use case of this gem is to handle 5 million+ keys that dumped into a 2GB file) and the keys are generated by something like the awesome resque-retry, resulting in strings that are not only long but also possibly filled with strange characters, you might want to use this tool to help cut the operation into multiple smaller chunks so you don't worry about escaping, Redis timeouts / service disruption, etc.

Installation

$ gem install redis-cleaner

or add to your Gemfile

gem 'redis-cleaner'

and install it with

$ bundle install

Example Usage

Instantiating a redis_cleaner

require 'redis'
require 'redis_cleaner'

# Timeout needs to be set, because KEYS is run on the Redis-server
# side, which is potentially slow -> O(n) where n is the number of keys
redis_config = {
  host: "127.0.0.1",
  port: 6390,
  timeout: 60
}

# Any Redis connection (as noted above) will do, here we just use the normal Redis RB client
redis_connection = Redis.new(redis_config)
redis_cleaner = RedisCleaner.new(redis_connection, "./borked_keys")

Separate dumping and cleaning

redis_cleaner.dump_matching_keys_to_temp_file("resque:resque-retry*") #<-- can be skipped if you already have a file to read from
cleanup_job_stats = redis_cleaner.delete_keys_in_temp_file(verbose: false)
puts "Deleted #{cleanup_job_stats[:deleted_keys_count]} keys out of #{cleanup_job_stats[:total_keys_count]}"

Do everything in one go (dumps to file and deletes the file by default)

redis_cleaner.dump_and_delete("resque:resque-retry*", delete_temp_file: false, verbose: false, batch_size: 200)

License

Copyright (c) 2013 by Lloyd Chan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.