class CacheJSON::Adapters::Redis

Attributes

args[RW]
options[RW]

Public Class Methods

new(args:, options:) click to toggle source
# File lib/cache_json/adapters/redis.rb, line 9
def initialize(args:, options:)
  @args = args
  @options = options
end

Public Instance Methods

cache_expiring_soon?() click to toggle source
# File lib/cache_json/adapters/redis.rb, line 34
def cache_expiring_soon?
  $redis.ttl(full_key) < options[:refresh][:buffer].to_i
end
cached_results() click to toggle source
# File lib/cache_json/adapters/redis.rb, line 14
def cached_results
  return @cached_results if @cached_results

  results_json = $redis.get(full_key)
  @cached_results = JSON.parse(results_json) if results_json
end
cached_results=(results) click to toggle source
# File lib/cache_json/adapters/redis.rb, line 21
def cached_results=(results)
  $redis.sadd(class_key, full_key)
  $redis.set(full_key, results.to_json)
  $redis.expire(full_key, options[:time_to_expire].to_i)
end
clear_cache!() click to toggle source
# File lib/cache_json/adapters/redis.rb, line 27
def clear_cache!
  $redis.smembers(class_key).each do |key|
    $redis.del(key)
  end
  $redis.del(class_key)
end

Private Instance Methods

arguments_signature() click to toggle source
# File lib/cache_json/adapters/redis.rb, line 48
def arguments_signature
  args.to_a.map do |row|
    "#{row[0]}:#{row[1]}"
  end.sort.join('-')
end
class_key() click to toggle source
# File lib/cache_json/adapters/redis.rb, line 44
def class_key
  @class_key ||= "CacheJSON-#{options[:class_name]}"
end
full_key() click to toggle source
# File lib/cache_json/adapters/redis.rb, line 40
def full_key
  @full_key ||= "#{class_key}-#{arguments_signature}"
end