Class: CacheJSON::Adapters::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_json/adapters/redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args:, options:) ⇒ Redis

Returns a new instance of Redis.



9
10
11
12
# File 'lib/cache_json/adapters/redis.rb', line 9

def initialize(args:, options:)
  @args = args
  @options = options
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



6
7
8
# File 'lib/cache_json/adapters/redis.rb', line 6

def args
  @args
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/cache_json/adapters/redis.rb', line 7

def options
  @options
end

Instance Method Details

#cache_expiring_soon?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/cache_json/adapters/redis.rb', line 34

def cache_expiring_soon?
  $redis.ttl(full_key) < options[:refresh][:buffer].to_i
end

#cached_resultsObject



14
15
16
17
18
19
# 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) ⇒ Object



21
22
23
24
25
# 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!Object



27
28
29
30
31
32
# 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