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.



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

def initialize(store_path)
  @store_path = store_path
  @in_txn = false
  @dirty = false
  load_data
end

Instance Method Details

#fetch(key) ⇒ Object



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

def fetch(key)
  raise(SetOutsideTransactionNotAllowed) unless @in_txn
  v = get(key)
  unless v
    @dirty = true
    v = yield
    @data[key] = v
  end
  v
end

#get(key) ⇒ Object



19
20
21
# File 'lib/bootsnap/load_path_cache/store.rb', line 19

def get(key)
  @data[key]
end

#set(key, value) ⇒ Object



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

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

#transactionObject



42
43
44
45
46
47
48
49
# File 'lib/bootsnap/load_path_cache/store.rb', line 42

def transaction
  raise(NestedTransactionError) if @in_txn
  @in_txn = true
  yield
ensure
  commit_transaction
  @in_txn = false
end