Class: SplitIoClient::Cache::Adapters::CacheAdapter
- Inherits:
-
Object
- Object
- SplitIoClient::Cache::Adapters::CacheAdapter
- Extended by:
- Forwardable
- Defined in:
- lib/splitclient-rb/cache/adapters/cache_adapter.rb
Instance Method Summary collapse
- #add_to_set(key, values) ⇒ Object
- #clear(namespace_key) ⇒ Object
- #delete(key) ⇒ Object
- #delete_from_set(key, fields) ⇒ Object
- #exists?(key) ⇒ Boolean
- #find_strings_by_prefix(prefix) ⇒ Object
- #get_set(key) ⇒ Object
- #in_set?(key, field) ⇒ Boolean
-
#initialize(config) ⇒ CacheAdapter
constructor
A new instance of CacheAdapter.
- #initialize_map(key) ⇒ Object
- #multiple_strings(keys) ⇒ Object
- #set_string(key, value) ⇒ Object
- #string(key) ⇒ Object
Constructor Details
#initialize(config) ⇒ CacheAdapter
Returns a new instance of CacheAdapter.
10 11 12 13 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 10 def initialize(config) @cache = LruRedux::TTL::ThreadSafeCache.new(config.max_cache_size, config.cache_ttl) @adapter = config.cache_adapter end |
Instance Method Details
#add_to_set(key, values) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 71 def add_to_set(key, values) if values.is_a? Array values.each { |value| add_to_map(key, value, 1) } else add_to_map(key, values, 1) end @adapter.add_to_set(key, values) end |
#clear(namespace_key) ⇒ Object
20 21 22 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 20 def clear(namespace_key) @cache.clear end |
#delete(key) ⇒ Object
15 16 17 18 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 15 def delete(key) @cache.delete(key) @adapter.delete(key) end |
#delete_from_set(key, fields) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 96 def delete_from_set(key, fields) cached_value = get(key) if cached_value if fields.is_a? Array fields.each { |field| cached_value.delete(field) } else cached_value.delete(fields) end end @adapter.delete_from_set(key, fields) end |
#exists?(key) ⇒ Boolean
62 63 64 65 66 67 68 69 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 62 def exists?(key) cached_value = get(key) if cached_value.nil? @adapter.exists?(key) else true end end |
#find_strings_by_prefix(prefix) ⇒ Object
58 59 60 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 58 def find_strings_by_prefix(prefix) @adapter.find_strings_by_prefix(prefix) end |
#get_set(key) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 88 def get_set(key) cached_value = get(key) if cached_value.nil? return @adapter.get_set(key) end cached_value.keys end |
#in_set?(key, field) ⇒ Boolean
80 81 82 83 84 85 86 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 80 def in_set?(key, field) cached_value = get(key) if cached_value.nil? return @adapter.in_set?(key, field) end cached_value.key?(field) end |
#initialize_map(key) ⇒ Object
109 110 111 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 109 def initialize_map(key) @cache[key] = Concurrent::Map.new end |
#multiple_strings(keys) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 37 def multiple_strings(keys) cached_values = keys.each_with_object({}) do |key, memo| memo[key] = get(key) end non_cached_keys = [] cached_values.delete_if{ |k,v| v.nil? ? non_cached_keys << k : false } if non_cached_keys.any? new_values = @adapter.multiple_strings(non_cached_keys) new_values.keys.each do |key, value| add(key, value) end cached_values.merge!(new_values) end cached_values end |
#set_string(key, value) ⇒ Object
32 33 34 35 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 32 def set_string(key, value) add(key, value) @adapter.set_string(key, value) end |
#string(key) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/splitclient-rb/cache/adapters/cache_adapter.rb', line 24 def string(key) value = get(key) return value if value value = @adapter.string(key) add(key, value) value end |