class PgParty::Cache

Constants

LOCK

Public Class Methods

new() click to toggle source
# File lib/pg_party/cache.rb, line 9
def initialize
  # automatically initialize a new hash when
  # accessing an object id that doesn't exist
  @store = Hash.new { |h, k| h[k] = { models: {}, partitions: nil, partitions_with_subpartitions: nil } }
end

Public Instance Methods

clear!() click to toggle source
# File lib/pg_party/cache.rb, line 15
def clear!
  LOCK.synchronize { @store.clear }

  nil
end
fetch_model(key, child_table, &block) click to toggle source
# File lib/pg_party/cache.rb, line 21
def fetch_model(key, child_table, &block)
  return block.call unless caching_enabled?

  LOCK.synchronize { fetch_value(@store[key][:models], child_table.to_sym, block) }
end
fetch_partitions(key, include_subpartitions, &block) click to toggle source
# File lib/pg_party/cache.rb, line 27
def fetch_partitions(key, include_subpartitions, &block)
  return block.call unless caching_enabled?
  sub_key = include_subpartitions ? :partitions_with_subpartitions : :partitions

  LOCK.synchronize { fetch_value(@store[key], sub_key, block) }
end

Private Instance Methods

caching_enabled?() click to toggle source
# File lib/pg_party/cache.rb, line 36
def caching_enabled?
  PgParty.config.caching
end
fetch_value(subhash, key, block) click to toggle source
# File lib/pg_party/cache.rb, line 40
def fetch_value(subhash, key, block)
  entry = subhash[key]

  if entry.nil? || entry.expired?
    entry = Entry.new(block.call)
    subhash[key] = entry
  end

  entry.value
end