Sha256: 4caff8823ffea25ebc3e0cdc82afe86ea646bd50445c1cd08cce9667c6c5b214

Contents?: true

Size: 668 Bytes

Versions: 4

Compression:

Stored size: 668 Bytes

Contents

# frozen_string_literal: true

require 'json'

module PansophyAuthenticator
  class Cache
    CACHE_KEY = 'pansophy_authenticator_application_keys'.freeze

    def initialize(cache_store)
      @cache_store = cache_store
    end

    def read
      JSON.parse(@cache_store.read(CACHE_KEY))
    end

    def write(value)
      @cache_store.write CACHE_KEY, JSON.dump(value)
    end

    def delete
      @cache_store.delete CACHE_KEY
    end

    def exist?
      @cache_store.exist? CACHE_KEY
    end

    def fetch
      return read if exist?
      return nil unless block_given?
      yield(CACHE_KEY).tap do |value|
        write(value)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pansophy_authenticator-0.5.0 lib/pansophy_authenticator/cache.rb
pansophy_authenticator-0.4.3 lib/pansophy_authenticator/cache.rb
pansophy_authenticator-0.4.2 lib/pansophy_authenticator/cache.rb
pansophy_authenticator-0.4.1 lib/pansophy_authenticator/cache.rb