Sha256: 2825eadede4398a79f092be073e1f6d131688746bbc42ecceac764172ca6e48d

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

module Storyblok
  module Cache
    class Redis
      DEFAULT_CONFIGURATION = {
        ttl: 60 * 60 * 24
      }

      def initialize(*args)
        options = args.last.is_a?(::Hash) ? args.pop : {}

        @redis = options.delete(:redis) || begin
          if defined?(::Redis)
            ::Redis.current
          else
            raise "Redis.current could not be found. Supply :redis option or make sure Redis.current is available."
          end
        end

        @options = DEFAULT_CONFIGURATION.merge(options)
      end

      def cache(key, expire = nil)
        if expire == 0
          return yield(self)
        end

        expire ||= @options[:ttl]

        if (value = get(key)).nil?
          value = yield(self)
          set(key, value, expire)
        end

        value
      end

      def get(key)
        @redis.get(key)
      end

      def set(key, value, expire = false)
        if expire
          @redis.setex(key, expire, value)
        else
          @redis.set(key, value)
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
storyblok-2.1.1 lib/storyblok/cache/redis.rb
storyblok-2.1.0 lib/storyblok/cache/redis.rb
storyblok-2.0.8 lib/storyblok/cache/redis.rb
storyblok-2.0.7 lib/storyblok/cache/redis.rb
storyblok-2.0.6 lib/storyblok/cache/redis.rb
storyblok-2.0.5 lib/storyblok/cache/redis.rb