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
- #available? ⇒ Boolean
- #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
- #monitoring_enabled? ⇒ Boolean
- #stop ⇒ Object
- #upsert(kind, item) ⇒ Object
Constructor Details
#initialize(core, opts) ⇒ CachingStoreWrapper
Creates a new store wrapper instance.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 35 def initialize(core, opts) @core = core expiration_seconds = opts[:expiration] || 15 if expiration_seconds > 0 capacity = opts[:capacity] || 1000 @cache = Impl::ExpiringCache.new(capacity, expiration_seconds) else @cache = nil end @inited = Concurrent::AtomicBoolean.new(false) @has_available_method = @core.respond_to? :available? end |
Instance Method Details
#all(kind) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 91 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 |
#available? ⇒ Boolean
54 55 56 57 58 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 54 def available? return false unless @has_available_method @core.available? end |
#delete(kind, key, version) ⇒ Object
111 112 113 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 111 def delete(kind, key, version) upsert(kind, { key: key, version: version, deleted: true }) end |
#get(kind, key) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 75 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 60 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 115 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 |
#monitoring_enabled? ⇒ Boolean
50 51 52 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 50 def monitoring_enabled? @has_available_method end |
#stop ⇒ Object
132 133 134 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 132 def stop @core.stop end |
#upsert(kind, item) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/ldclient-rb/integrations/util/store_wrapper.rb', line 102 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 |