module CatchCache::Cache

Public Class Methods

catch_then_cache(redis_key, &block) click to toggle source
# File lib/catch_cache/cache.rb, line 6
def self.catch_then_cache(redis_key, &block)
  redis = Redis.new
  val = redis.get(redis_key)

  # retrieve the cache with redis_key as its key
  cache = JSON.parse(val.blank? ? "[]" : val)

  if cache.blank?
    timeline_logs = block.call
    redis.set(redis_key, timeline_logs.to_json)
    cache = JSON.parse(redis.get(redis_key))
  end

  cache
end
included(klass) click to toggle source
# File lib/catch_cache/cache.rb, line 4
def included(klass)
  klass.class_eval do
    def self.catch_then_cache(redis_key, &block)
      redis = Redis.new
      val = redis.get(redis_key)

      # retrieve the cache with redis_key as its key
      cache = JSON.parse(val.blank? ? "[]" : val)

      if cache.blank?
        timeline_logs = block.call
        redis.set(redis_key, timeline_logs.to_json)
        cache = JSON.parse(redis.get(redis_key))
      end

      cache
    end

    def catch_then_cache(redis_key, &block)
      redis = Redis.new
      val = redis.get(redis_key)

      # retrieve the cache with redis_key as its key
      cache = JSON.parse(val.blank? ? "[]" : val)

      if cache.blank?
        timeline_logs = block.call
        redis.set(redis_key, timeline_logs.to_json)
        cache = JSON.parse(redis.get(redis_key))
      end

      cache
    end
  end
end