Sha256: f998e6ab84569ea6b79ed2e1089f0bdae756abcb72f3cde26eaa365fcce0e856

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

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

      def initialize(*args)
        options = args.extract_options!

        @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

5 entries across 5 versions & 1 rubygems

Version Path
storyblok-2.0.4 lib/storyblok/cache/redis.rb
storyblok-2.0.3 lib/storyblok/cache/redis.rb
storyblok-2.0.2 lib/storyblok/cache/redis.rb
storyblok-2.0.1 lib/storyblok/cache/redis.rb
storyblok-2.0.0 lib/storyblok/cache/redis.rb