Class: QPixel::NamespacedEnvCache

Inherits:
ActiveSupport::Cache::Store
  • Object
show all
Defined in:
lib/namespaced_env_cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(underlying) ⇒ NamespacedEnvCache

Returns a new instance of NamespacedEnvCache.



3
4
5
6
# File 'lib/namespaced_env_cache.rb', line 3

def initialize(underlying)
  @underlying = underlying
  @getters = {}
end

Class Method Details

.supports_cache_versioning?Boolean

We have to statically report that we support cache versioning even though this depends on the underlying class. However, this is not really a problem since all cache stores provided by activesupport support the feature and we only use the redis cache (by activesupport) for QPixel.

Returns:

  • (Boolean)


78
79
80
# File 'lib/namespaced_env_cache.rb', line 78

def self.supports_cache_versioning?
  true
end

Instance Method Details

#fetch_multi(*keys, **opts, &block) ⇒ Object



45
46
47
48
49
# File 'lib/namespaced_env_cache.rb', line 45

def fetch_multi(*keys, **opts, &block)
  include_community = include_community(opts)
  keys = keys.map { |k| construct_ns_key(k, include_community: include_community) }
  @underlying.fetch_multi(*keys, **opts, &block)
end

#include_community(opts) ⇒ Object



8
9
10
11
# File 'lib/namespaced_env_cache.rb', line 8

def include_community(opts)
  include = opts.delete(:include_community)
  include.nil? ? true : include
end

#persistent(name, **opts, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/namespaced_env_cache.rb', line 51

def persistent(name, **opts, &block)
  namespaced = construct_ns_key(name, include_community: false)
  if block_given?
    @getters[namespaced] = block
  end

  if opts && opts[:clear] == true
    @underlying.delete namespaced
  end

  value = @underlying.read namespaced
  if value.nil?
    if @getters.include? namespaced
      value = @getters[namespaced].call
      @underlying.write namespaced, value
      value
    else
      raise NotImplementedError, 'No config value was available and no block was given'
    end
  else
    value
  end
end

#read_multi(*keys, **opts) ⇒ Object



38
39
40
41
42
43
# File 'lib/namespaced_env_cache.rb', line 38

def read_multi(*keys, **opts)
  include_community = include_community(opts)
  keys = keys.map { |k| [construct_ns_key(k, include_community: include_community), k] }.to_h
  results = @underlying.read_multi(*keys.keys, **opts)
  results.map { |k, v| [keys[k], v] }.to_h
end