Class: LaunchDarkly::Impl::Integrations::Redis::RedisFeatureStoreCore Private

Inherits:
RedisStoreImplBase
  • Object
show all
Defined in:
lib/ldclient-rb/impl/integrations/redis_impl.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Internal implementation of the Redis feature store, intended to be used with CachingStoreWrapper.

Since:

  • 5.5.0

Instance Method Summary collapse

Methods inherited from RedisStoreImplBase

#stop

Constructor Details

#initialize(opts) ⇒ RedisFeatureStoreCore

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RedisFeatureStoreCore.

Since:

  • 5.5.0



164
165
166
167
168
# File 'lib/ldclient-rb/impl/integrations/redis_impl.rb', line 164

def initialize(opts)
  super(opts)

  @test_hook = opts[:test_hook]  # used for unit tests, deliberately undocumented
end

Instance Method Details

#available?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • 5.5.0



170
171
172
173
174
175
176
# File 'lib/ldclient-rb/impl/integrations/redis_impl.rb', line 170

def available?
  # We don't care what the status is, only that we can connect
  initialized_internal?
  true
rescue
  false
end

#descriptionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



178
179
180
# File 'lib/ldclient-rb/impl/integrations/redis_impl.rb', line 178

def description
  "RedisFeatureStore"
end

#get_all_internal(kind) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



205
206
207
208
209
210
211
212
213
214
# File 'lib/ldclient-rb/impl/integrations/redis_impl.rb', line 205

def get_all_internal(kind)
  fs = {}
  with_connection do |redis|
    hashfs = redis.hgetall(items_key(kind))
    hashfs.each do |k, json_item|
      fs[k.to_sym] = Model.deserialize(kind, json_item)
    end
  end
  fs
end

#get_internal(kind, key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



199
200
201
202
203
# File 'lib/ldclient-rb/impl/integrations/redis_impl.rb', line 199

def get_internal(kind, key)
  with_connection do |redis|
    get_redis(redis, kind, key)
  end
end

#init_internal(all_data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ldclient-rb/impl/integrations/redis_impl.rb', line 182

def init_internal(all_data)
  count = 0
  with_connection do |redis|
    redis.multi do |multi|
      all_data.each do |kind, items|
        multi.del(items_key(kind))
        count = count + items.count
        items.each do |key, item|
          multi.hset(items_key(kind), key, Model.serialize(kind,item))
        end
      end
      multi.set(inited_key, inited_key)
    end
  end
  @logger.info { "RedisFeatureStore: initialized with #{count} items" }
end

#initialized_internal?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • 5.5.0



248
249
250
# File 'lib/ldclient-rb/impl/integrations/redis_impl.rb', line 248

def initialized_internal?
  with_connection { |redis| redis.exists?(inited_key) }
end

#upsert_internal(kind, new_item) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ldclient-rb/impl/integrations/redis_impl.rb', line 216

def upsert_internal(kind, new_item)
  base_key = items_key(kind)
  key = new_item[:key]
  try_again = true
  final_item = new_item
  while try_again
    try_again = false
    with_connection do |redis|
      redis.watch(base_key) do
        old_item = get_redis(redis, kind, key)
        before_update_transaction(base_key, key)
        if old_item.nil? || old_item[:version] < new_item[:version]
          result = redis.multi do |multi|
            multi.hset(base_key, key, Model.serialize(kind, new_item))
          end
          if result.nil?
            @logger.debug { "RedisFeatureStore: concurrent modification detected, retrying" }
            try_again = true
          end
        else
          final_item = old_item
          action = new_item[:deleted] ? "delete" : "update"
          # rubocop:disable Layout/LineLength
          @logger.warn { "RedisFeatureStore: attempted to #{action} #{key} version: #{old_item[:version]} in '#{kind[:namespace]}' with a version that is the same or older: #{new_item[:version]}" }
        end
        redis.unwatch
      end
    end
  end
  final_item
end