Sha256: 8b41e057a1334c2d94411bf4edcd0060dd27e58972bd43bcc9d47559d24e9595

Contents?: true

Size: 755 Bytes

Versions: 2

Compression:

Stored size: 755 Bytes

Contents

require 'redis-namespace'
require 'oj'

module TwitterCache
  class Redis
    include TwitterCache::Helpers
    attr_reader :client

    def initialize
      redis_url = ENV['REDIS_URL'] || config.redis
      redis = ::Redis.new(url: redis_url)
      @client = ::Redis::Namespace.new(:twitter_cache_gem, 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(ttl) if ttl
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitter-cache-0.1.1 lib/twitter_cache/redis.rb
twitter-cache-0.1.0 lib/twitter_cache/redis.rb