Class: PgParty::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_party/cache.rb

Constant Summary collapse

LOCK =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



9
10
11
12
13
# 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

Instance Method Details

#clear!Object



15
16
17
18
19
# File 'lib/pg_party/cache.rb', line 15

def clear!
  LOCK.synchronize { @store.clear }

  nil
end

#fetch_model(key, child_table, &block) ⇒ Object



21
22
23
24
25
# 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) ⇒ Object



27
28
29
30
31
32
# 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