Class: LaunchDarkly::Integrations::Util::CachingStoreWrapper
- Inherits:
-
Object
- Object
- LaunchDarkly::Integrations::Util::CachingStoreWrapper
- Includes:
- LaunchDarkly::Interfaces::FeatureStore
- Defined in:
- lib/ldclient-rb/integrations/util/store_wrapper.rb
Overview
CachingStoreWrapper is a partial implementation of the LaunchDarkly::Interfaces::FeatureStore pattern that delegates part of its behavior to another object, while providing optional caching behavior and other logic that would otherwise be repeated in every feature store implementation. This makes it easier to create new database integrations by implementing only the database-specific logic.
The mixin FeatureStoreCore describes the methods that need to be supported by the inner implementation object.
Instance Method Summary collapse
- #all(kind) ⇒ Object
- #delete(kind, key, version) ⇒ Object
- #get(kind, key) ⇒ Object
- #init(all_data) ⇒ Object
-
#initialize(core, opts) ⇒ CachingStoreWrapper
constructor
Creates a new store wrapper instance.
- #initialized? ⇒ Boolean
- #stop ⇒ Object
- #upsert(kind, item) ⇒ Object
Constructor Details
#initialize(core, opts) ⇒ CachingStoreWrapper
Creates a new store wrapper instance.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 34 def initialize(core, opts) @core = core expiration_seconds = opts[:expiration] || 15 if expiration_seconds > 0 capacity = opts[:capacity] || 1000 @cache = ExpiringCache.new(capacity, expiration_seconds) else @cache = nil end @inited = Concurrent::AtomicBoolean.new(false) end |
Instance Method Details
#all(kind) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 79 def all(kind) unless @cache.nil? items = @cache[all_cache_key(kind)] return items unless items.nil? end items = items_if_not_deleted(@core.get_all_internal(kind)) @cache[all_cache_key(kind)] = items unless @cache.nil? items end |
#delete(kind, key, version) ⇒ Object
99 100 101 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 99 def delete(kind, key, version) upsert(kind, { key: key, version: version, deleted: true }) end |
#get(kind, key) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 63 def get(kind, key) unless @cache.nil? cache_key = item_cache_key(kind, key) cached = @cache[cache_key] # note, item entries in the cache are wrapped in an array so we can cache nil values return item_if_not_deleted(cached[0]) unless cached.nil? end item = @core.get_internal(kind, key) unless @cache.nil? @cache[cache_key] = [item] end item_if_not_deleted(item) end |
#init(all_data) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 48 def init(all_data) @core.init_internal(all_data) @inited.make_true unless @cache.nil? @cache.clear all_data.each do |kind, items| @cache[kind] = items_if_not_deleted(items) items.each do |key, item| @cache[item_cache_key(kind, key)] = [item] end end end end |
#initialized? ⇒ Boolean
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 103 def initialized? return true if @inited.value if @cache.nil? result = @core.initialized_internal? else result = @cache[inited_cache_key] if result.nil? result = @core.initialized_internal? @cache[inited_cache_key] = result end end @inited.make_true if result result end |
#stop ⇒ Object
120 121 122 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 120 def stop @core.stop end |
#upsert(kind, item) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 90 def upsert(kind, item) new_state = @core.upsert_internal(kind, item) unless @cache.nil? @cache[item_cache_key(kind, item[:key])] = [new_state] @cache.delete(all_cache_key(kind)) end end |