Sha256: de77d8b1648621c0495049eb78e6646febb94660fb17deca924a5e23393566ee

Contents?: true

Size: 1.04 KB

Versions: 16

Compression:

Stored size: 1.04 KB

Contents

require_relative 'base'
require 'redis'
require 'json'

module Zipcoder
  module Cacher
    class Redis < Base

      # This is here for stubbing
      def self._create_redis_client(**kwargs)
        ::Redis.new(**kwargs)
      end

      def _init_cache(**kwargs)
        @redis = self.class._create_redis_client(**kwargs)
      end

      def _empty_cache
        keys = @redis.keys("#{KEY_BASE}*")
        @redis.del(*keys) unless keys.empty?
      end

      def _write_cache(key, value)
        return if value == nil
        @redis.set(key, value.to_json)
      end

      def _read_cache(key)
        data = @redis.get(key)
        data == nil ? nil : JSON.parse(data, :symbolize_names => true)
      end

      def _iterate_keys(**kwargs, &block)
        return if block == nil

        start_with = kwargs[:start_with] || KEY_BASE

        # Redis "keys" command will pre-filter the keys for us so no
        # need for "if" statement
        @redis.keys("#{start_with}*").each do |key|
          block.call(key)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
zipcoder-0.9.2 lib/zipcoder/cacher/redis.rb
zipcoder-0.9.1 lib/zipcoder/cacher/redis.rb
zipcoder-0.9.0 lib/zipcoder/cacher/redis.rb
zipcoder-0.8.4 lib/zipcoder/cacher/redis.rb
zipcoder-0.8.3 lib/zipcoder/cacher/redis.rb
zipcoder-0.8.2 lib/zipcoder/cacher/redis.rb
zipcoder-0.8.1 lib/zipcoder/cacher/redis.rb
zipcoder-0.8.0 lib/zipcoder/cacher/redis.rb
zipcoder-0.7.4 lib/zipcoder/cacher/redis.rb
zipcoder-0.7.3 lib/zipcoder/cacher/redis.rb
zipcoder-0.7.2 lib/zipcoder/cacher/redis.rb
zipcoder-0.7.1 lib/zipcoder/cacher/redis.rb
zipcoder-0.7.0 lib/zipcoder/cacher/redis.rb
zipcoder-0.6.1 lib/zipcoder/cacher/redis.rb
zipcoder-0.6.0 lib/zipcoder/cacher/redis.rb
zipcoder-0.5.0 lib/zipcoder/cacher/redis.rb