Sha256: 5cf3398c1c1dfce0d5d4cce0e8b1aee0d24f013c3fe57d7a47386262d12a3e30

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module Gretel
  module Trail
    class RedisStore < Store
      class << self
        # Options to connect to Redis.
        def connect_options
          @connect_options ||= {}
        end

        # Sets the Redis connect options.
        attr_writer :connect_options

        # Number of seconds to keep the trails in Redis.
        # Default: +1.day+
        def expires_in
          @expires_in ||= 1.day
        end

        # Sets the number of seconds to keep the trails in Redis.
        attr_writer :expires_in

        # Save array to Redis.
        def save(array)
          json = array.to_json
          key = Digest::SHA1.hexdigest(json)
          redis.setex redis_key_for(key), expires_in, json
          key
        end

        # Retrieve array from Redis.
        def retrieve(key)
          if json = redis.get(redis_key_for(key))
            JSON.parse(json)
          end
        end

        # Reference to the Redis connection.
        def redis
          @redis ||= begin
            raise "Redis needs to be installed in order for #{name} to use it. Please add `gem \"redis\"` to your Gemfile." unless defined?(Redis)
            Redis.new(connect_options)
          end
        end

        private

        # Key to be stored in Redis.
        def redis_key_for(key)
          "gretel:trail:#{key}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gretel-3.0.0.beta4 lib/gretel/trail/stores/redis_store.rb
gretel-3.0.0.beta3 lib/gretel/trail/stores/redis_store.rb