Sha256: 1d7a62415f55c02a76f6d0a605f294a4ef6e48e3f90e6ce3f612f6e5d0b64239

Contents?: true

Size: 801 Bytes

Versions: 30

Compression:

Stored size: 801 Bytes

Contents

# frozen_string_literal: true

module UmbrellioUtils
  module Store
    extend self

    include Memery

    def []=(key, value)
      attrs = { key: key.to_s, value: JSON.dump(value), updated_at: Time.current }
      entry.upsert_dataset.insert(attrs)
      clear_cache_for(key)
    end

    def [](key)
      find(key)&.value
    end

    def delete(key)
      result = !!find(key)&.delete
      clear_cache_for(key) if result
      result
    end

    def find(key)
      Rails.cache.fetch(cache_key_for(key)) { entry[key.to_s] }
    end

    memoize def entry
      Sequel::Model(UmbrellioUtils.config.store_table_name)
    end

    private

    def cache_key_for(key)
      "store-entry-#{key}"
    end

    def clear_cache_for(key)
      Rails.cache.delete(cache_key_for(key))
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
umbrellio-utils-0.5.0 lib/umbrellio_utils/store.rb
umbrellio-utils-0.4.3 lib/umbrellio_utils/store.rb
umbrellio-utils-0.4.2 lib/umbrellio_utils/store.rb
umbrellio-utils-0.4.1 lib/umbrellio_utils/store.rb
umbrellio-utils-0.4.0 lib/umbrellio_utils/store.rb
umbrellio-utils-0.3.2 lib/umbrellio_utils/store.rb
umbrellio-utils-0.3.1 lib/umbrellio_utils/store.rb
umbrellio-utils-0.3.0 lib/umbrellio_utils/store.rb
umbrellio-utils-0.2.0 lib/umbrellio_utils/store.rb
umbrellio-utils-0.1.0 lib/umbrellio_utils/store.rb