Sha256: 9bd8e580098f3f56aa97e0910373092f29d762f2f266009f2cf9907a22c775d5

Contents?: true

Size: 824 Bytes

Versions: 2

Compression:

Stored size: 824 Bytes

Contents

require 'redis-namespace'
require 'oj'

module Twitter
  module Cache
    class Redis
      include Twitter::Cache::Helpers
      attr_reader :client

      def initialize
        redis_url = ENV['REDIS_URL'] || config.redis
        redis = ::Redis.new(url: redis_url)
        @client = ::Redis::Namespace.new(config.namespace, redis: redis)
      end

      def method_missing(name, *args)
        client.send(name, *args)
      end

      def get(key, ttl: nil)
        stored = client.get(key)
        return Oj.load(stored, symbolize_keys: true) if stored
        return nil unless block_given?

        value = yield
        set(key, value, ttl: ttl)
        value
      end

      def set(key, value, ttl: nil)
        client.set(key, Oj.dump(value))
        client.expire(key, ttl) if ttl
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitter-cache-0.2.1 lib/twitter/cache/redis.rb
twitter-cache-0.2.0 lib/twitter/cache/redis.rb