Class: Bootsnap::LoadPathCache::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/bootsnap/load_path_cache/store.rb

Constant Summary collapse

NestedTransactionError =
Class.new(StandardError)
SetOutsideTransactionNotAllowed =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(store_path) ⇒ Store

Returns a new instance of Store.



13
14
15
16
17
18
19
# File 'lib/bootsnap/load_path_cache/store.rb', line 13

def initialize(store_path)
  @store_path = store_path
  # TODO: Remove conditional once Ruby 2.2 support is dropped.
  @txn_mutex =  defined?(::Mutex) ? ::Mutex.new : ::Thread::Mutex.new
  @dirty = false
  load_data
end

Instance Method Details

#fetch(key) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/bootsnap/load_path_cache/store.rb', line 25

def fetch(key)
  raise(SetOutsideTransactionNotAllowed) unless @txn_mutex.owned?
  v = get(key)
  unless v
    @dirty = true
    v = yield
    @data[key] = v
  end
  v
end

#get(key) ⇒ Object



21
22
23
# File 'lib/bootsnap/load_path_cache/store.rb', line 21

def get(key)
  @data[key]
end

#set(key, value) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/bootsnap/load_path_cache/store.rb', line 36

def set(key, value)
  raise(SetOutsideTransactionNotAllowed) unless @txn_mutex.owned?
  if value != @data[key]
    @dirty = true
    @data[key] = value
  end
end

#transactionObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/bootsnap/load_path_cache/store.rb', line 44

def transaction
  raise(NestedTransactionError) if @txn_mutex.owned?
  @txn_mutex.synchronize do
    begin
      yield
    ensure
      commit_transaction
    end
  end
end