Module: JsonSchematize::Cache::ClassMethods

Defined in:
lib/json_schematize/cache/class_methods.rb

Constant Summary collapse

DEFAULT_ONE_MIN =
60 * 60
DEFAULT_ONE_HOUR =
DEFAULT_ONE_MIN * 60
DEFAULT_ONE_DAY =
DEFAULT_ONE_HOUR * 24
DEFAULT_URL =
ENV["CACHE_LAYER_REDIS_URL"] || ENV["REDIS_URL"]
DEFAULTS =
{
  redis_url: DEFAULT_URL,
  ttl: DEFAULT_ONE_DAY,
  key: ->(val, _custom_key) { val.hash },
  update_on_change: true,
  redis_client: ->() { ::Redis.new(url: DEFAULT_URL) },
  stochastic_cache_bust: 0.8,
}

Instance Method Summary collapse

Instance Method Details

#cache_configurationObject



36
37
38
# File 'lib/json_schematize/cache/class_methods.rb', line 36

def cache_configuration
  @cache_configuration ||= DEFAULTS.clone
end

#cache_namespaceObject



28
29
30
# File 'lib/json_schematize/cache/class_methods.rb', line 28

def cache_namespace
   cache_configuration[:cache_namespace] ||= "jss:#{self.name.downcase}"
end

#cache_namespace=(namespace) ⇒ Object



32
33
34
# File 'lib/json_schematize/cache/class_methods.rb', line 32

def cache_namespace=(namespace)
  cache_configuration[:cache_namespace] = namespace
end

#cache_options(key: nil, redis_url: nil, redis_client: nil, cache_namespace: nil, ttl: nil, update_on_change: nil, stochastic_cache_bust: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/json_schematize/cache/class_methods.rb', line 17

def cache_options(key: nil, redis_url: nil, redis_client: nil, cache_namespace: nil, ttl: nil, update_on_change: nil, stochastic_cache_bust: nil)
  cache_configuration[:key] = key if key
  cache_configuration[:ttl] = ttl if ttl
  cache_configuration[:stochastic_cache_bust] = stochastic_cache_bust if stochastic_cache_bust
  cache_configuration[:update_on_change] = update_on_change if update_on_change
  cache_namespace = cache_configuration[:cache_namespace] = cache_namespace if cache_namespace

  self.redis_client = cache_configuration[:redis_client] = redis_client if redis_client
  self.redis_url = cache_configuration[:redis_url] = redis_url if redis_url
end

#cached_items(key_includes: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/json_schematize/cache/class_methods.rb', line 58

def cached_items(key_includes: nil)
  clear_unscored_items! if rand > cache_configuration[:stochastic_cache_bust]

  cached_keys.map do |key|
    if key_includes
      next unless key.include?(key_includes)
    end

    serialized_string = redis_client.get(key)
    Marshal.load(serialized_string)
  end.compact
end

#cached_keysObject



53
54
55
56
# File 'lib/json_schematize/cache/class_methods.rb', line 53

def cached_keys
  max_length = Time.now.to_i + cache_configuration[:ttl].to_i + 10
  redis_client.zrangebyscore(cache_namespace, Time.now.to_i, "+inf")
end

#clear_cache!Object



71
72
73
74
# File 'lib/json_schematize/cache/class_methods.rb', line 71

def clear_cache!
  redis_client.unlink(*cached_keys) if cached_keys.length > 0
  redis_client.unlink(cache_namespace)
end

#clear_unscored_items!Object



76
77
78
# File 'lib/json_schematize/cache/class_methods.rb', line 76

def clear_unscored_items!
  redis_client.zremrangebyscore(cache_namespace, "-inf", Time.now.to_i)
end

#redis_clientObject



49
50
51
# File 'lib/json_schematize/cache/class_methods.rb', line 49

def redis_client
  cache_configuration[:redis_client].is_a?(Proc) ? cache_configuration[:redis_client].call : cache_configuration[:redis_client]
end

#redis_client=(client) ⇒ Object



40
41
42
# File 'lib/json_schematize/cache/class_methods.rb', line 40

def redis_client=(client)
  cache_configuration[:redis_client] = client
end

#redis_url=(url) ⇒ Object



44
45
46
47
# File 'lib/json_schematize/cache/class_methods.rb', line 44

def redis_url=(url)
  cache_configuration[:redis_url] = url
  cache_configuration[:redis_client] = ::Redis.new(url: url)
end