class LaunchDarkly::InMemoryFeatureStore

Default implementation of the LaunchDarkly client's feature store, using an in-memory cache. This object holds feature flags and related data received from LaunchDarkly. Database-backed implementations are available in {LaunchDarkly::Integrations}.

Public Class Methods

new() click to toggle source
# File lib/ldclient-rb/in_memory_store.rb, line 34
def initialize
  @items = Hash.new
  @lock = Concurrent::ReadWriteLock.new
  @initialized = Concurrent::AtomicBoolean.new(false)
end

Public Instance Methods

all(kind) click to toggle source
# File lib/ldclient-rb/in_memory_store.rb, line 48
def all(kind)
  @lock.with_read_lock do
    coll = @items[kind]
    (coll.nil? ? Hash.new : coll).select { |_k, f| not f[:deleted] }
  end
end
delete(kind, key, version) click to toggle source
# File lib/ldclient-rb/in_memory_store.rb, line 55
def delete(kind, key, version)
  @lock.with_write_lock do
    coll = @items[kind]
    if coll.nil?
      coll = Hash.new
      @items[kind] = coll
    end
    old = coll[key.to_sym]

    if old.nil? || old[:version] < version
      coll[key.to_sym] = { deleted: true, version: version }
    end
  end
end
get(kind, key) click to toggle source
# File lib/ldclient-rb/in_memory_store.rb, line 40
def get(kind, key)
  @lock.with_read_lock do
    coll = @items[kind]
    f = coll.nil? ? nil : coll[key.to_sym]
    (f.nil? || f[:deleted]) ? nil : f
  end
end
init(all_data) click to toggle source
# File lib/ldclient-rb/in_memory_store.rb, line 70
def init(all_data)
  @lock.with_write_lock do
    @items.replace(all_data)
    @initialized.make_true
  end
end
initialized?() click to toggle source
# File lib/ldclient-rb/in_memory_store.rb, line 92
def initialized?
  @initialized.value
end
stop() click to toggle source
# File lib/ldclient-rb/in_memory_store.rb, line 96
def stop
  # nothing to do
end
upsert(kind, item) click to toggle source
# File lib/ldclient-rb/in_memory_store.rb, line 77
def upsert(kind, item)
  @lock.with_write_lock do
    coll = @items[kind]
    if coll.nil?
      coll = Hash.new
      @items[kind] = coll
    end
    old = coll[item[:key].to_sym]

    if old.nil? || old[:version] < item[:version]
      coll[item[:key].to_sym] = item
    end
  end
end