Sha256: 827555b0dbee5a157dcc7cdce418e45d13c2e86202a0da9029a06273cf071d1d

Contents?: true

Size: 944 Bytes

Versions: 15

Compression:

Stored size: 944 Bytes

Contents

# frozen_string_literal: true
class Condenser::CacheStore

  def fetch(key)
    value = get(key)
    
    if value.nil?
      value = yield
      set(key, Marshal.dump(value))
    else
      value = Marshal.load(value)
    end
    value
  end
  
  # Try to fetch key_or_proc if key exists.
  #
  # If the key exist it will fetch key_or_proc from the cache
  #
  # If key does not exists run the block and store results under key_or_proc.
  def fetch_if(key_or_proc, key, &block)
    if get(key)
      key_or_proc = key_or_proc.call if key_or_proc.is_a?(Proc)
      value = fetch(key_or_proc, &block)
    else
      value = block.call
      key_or_proc = key_or_proc.call if key_or_proc.is_a?(Proc)
      set(key_or_proc, Marshal.dump(value))
    end
    value
  end
  
  def delete(key)
    @key_access.delete(key)
    if value = @cache.delete(key)
      @cache_size -= cached_size(key, value)
      true
    else
      false
    end
  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
condenser-1.4 lib/condenser/cache_store.rb
condenser-1.3 lib/condenser/cache_store.rb
condenser-1.2 lib/condenser/cache_store.rb
condenser-1.0 lib/condenser/cache_store.rb
condenser-1.0.rc1 lib/condenser/cache_store.rb
condenser-0.3 lib/condenser/cache_store.rb
condenser-0.2 lib/condenser/cache_store.rb
condenser-0.1 lib/condenser/cache_store.rb
condenser-0.0.12 lib/condenser/cache_store.rb
condenser-0.0.11 lib/condenser/cache_store.rb
condenser-0.0.10 lib/condenser/cache_store.rb
condenser-0.0.9 lib/condenser/cache_store.rb
condenser-0.0.8 lib/condenser/cache_store.rb
condenser-0.0.7 lib/condenser/cache_store.rb
condenser-0.0.5 lib/condenser/cache_store.rb