Flush Redis cache using Ruby client

Flush all the redis cache keys

  • Install redis ruby client library
gem install redis
require 'redis'
  • Include the method, redis_connect before each test scenario in-order to clear redis cache.
redis_connect("your_host_name", xxxx)
def redis_connect(host, port)
 redis = Redis.new(host: "#{host}", port: port)
 redis.flushall
end
  • The same can be achieved with Linux library in terminal, redis-cli.
  • Install redis-cli
sudo yum install -y gcc
wget http://download.redis.io/redis-stable.tar.gz && tar xvzf redis-stable.tar.gz && cd redis-stable && make
sudo cp src/redis-cli /usr/bin/
redis-cli -h your_host_name flushall

Delete specific redis key from the list 

def redis_connect(host, port)
  redis = Redis.new(host: "#{host}", port: port)

  redis.keys.each do |k|
    
    if k.size == 10
      redis.del k
    end

    if k.include? "text"
      redis.del k
    end

  end
end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s