Class: Puma::Acme::DiskStore

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/acme/disk_store.rb

Overview

DiskStore is a simple key/value store that persists to disk using PStore.

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ DiskStore

Returns a new instance of DiskStore.



7
8
9
10
# File 'lib/puma/acme/disk_store.rb', line 7

def initialize(dir)
  path = File.join(dir, 'puma-acme.pstore')
  @pstore = PStore.new(path, true)
end

Instance Method Details

#delete(key, _options = nil) ⇒ Object



12
13
14
15
16
# File 'lib/puma/acme/disk_store.rb', line 12

def delete(key, _options = nil)
  @pstore.transaction do
    !!@pstore.delete(key)
  end
end

#fetch(key, _options = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/puma/acme/disk_store.rb', line 18

def fetch(key, _options = nil, &block)
  raise ArgumentError if block.nil?

  @pstore.transaction do
    @pstore[key] || (@pstore[key] = yield(key))
  end
end

#read(key, _options = nil) ⇒ Object



26
27
28
29
30
# File 'lib/puma/acme/disk_store.rb', line 26

def read(key, _options = nil)
  @pstore.transaction(true) do
    @pstore[key]
  end
end

#write(key, value, _options = nil) ⇒ Object



32
33
34
35
36
37
# File 'lib/puma/acme/disk_store.rb', line 32

def write(key, value, _options = nil)
  @pstore.transaction do
    @pstore[key] = value
    true
  end
end