Sha256: 60dd91e3ec31ef5451c2b993cc9cba6cfd4b53f5db569af37ad5d11ce079cc77

Contents?: true

Size: 976 Bytes

Versions: 4

Compression:

Stored size: 976 Bytes

Contents

require 'redis'
require 'flipper'
require 'flipper/adapters/cache_base'

module Flipper
  module Adapters
    # Public: Adapter that wraps another adapter with the ability to cache
    # adapter calls in Redis.
    class RedisCache < CacheBase
      def initialize(adapter, cache, ttl = 3600, prefix: nil)
        super
      end

      private

      def cache_fetch(key, &block)
        cached = @cache.get(key)
        if cached
          Marshal.load(cached)
        else
          to_cache = yield
          cache_write key, to_cache
          to_cache
        end
      end

      def cache_read_multi(keys)
        return {} if keys.empty?

        values = @cache.mget(*keys).map do |value|
          value ? Marshal.load(value) : nil
        end

        Hash[keys.zip(values)]
      end

      def cache_write(key, value)
        @cache.setex(key, @ttl, Marshal.dump(value))
      end

      def cache_delete(key)
        @cache.del(key)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
flipper-redis-1.3.2 lib/flipper/adapters/redis_cache.rb
flipper-redis-1.3.1 lib/flipper/adapters/redis_cache.rb
flipper-redis-1.3.0 lib/flipper/adapters/redis_cache.rb
flipper-redis-1.3.0.pre lib/flipper/adapters/redis_cache.rb