Class: Shhh::App::Password::Cache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/shhh/app/password/cache.rb

Constant Summary collapse

URI =
'druby://127.0.0.1:24924'
DEFAULT_TIMEOUT =
300
TRIES =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



15
16
17
# File 'lib/shhh/app/password/cache.rb', line 15

def enabled
  @enabled
end

#providerObject

Returns the value of attribute provider.



15
16
17
# File 'lib/shhh/app/password/cache.rb', line 15

def provider
  @provider
end

#timeoutObject

Returns the value of attribute timeout.



15
16
17
# File 'lib/shhh/app/password/cache.rb', line 15

def timeout
  @timeout
end

Instance Method Details

#[](key) ⇒ Object



43
44
45
46
47
48
# File 'lib/shhh/app/password/cache.rb', line 43

def [] (key)
  cache = self
  operation do
    cache.provider.read(cache.md5(key))
  end
end

#[]=(key, value) ⇒ Object



50
51
52
53
54
55
# File 'lib/shhh/app/password/cache.rb', line 50

def []=(key, value)
  cache = self
  operation do
    cache.provider.write(cache.md5(key), value, cache.timeout)
  end
end

#configure(provider: Coin, enabled: true, timeout: DEFAULT_TIMEOUT) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/shhh/app/password/cache.rb', line 17

def configure(provider: Coin, enabled: true, timeout: DEFAULT_TIMEOUT)
  Coin.uri = URI if provider == Coin

  self.provider = provider
  self.enabled  = enabled
  self.timeout  = timeout

  self
end

#md5(string) ⇒ Object



57
58
59
# File 'lib/shhh/app/password/cache.rb', line 57

def md5(string)
  Digest::MD5.base64digest(string)
end

#operationObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shhh/app/password/cache.rb', line 29

def operation
  retries ||= TRIES
  yield if self.enabled
rescue StandardError => e
  if retries == TRIES && Coin.remote_uri.nil?
    Coin.remote_uri = URI if provider == Coin
    retries         -= 1
    retry
  end
  puts 'WARNING: error reading from DRB server: ' + e.message.red
  nil
end