Class: Terminalwire::Cache::File::Entry

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

Constant Summary collapse

VERSION =
"1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ Entry

Returns a new instance of Entry.



50
51
52
53
# File 'lib/terminalwire/cache.rb', line 50

def initialize(path:)
  @path = path
  deserialize if persisted?
end

Instance Attribute Details

#expiresObject

Returns the value of attribute expires.



48
49
50
# File 'lib/terminalwire/cache.rb', line 48

def expires
  @expires
end

#valueObject

Returns the value of attribute value.



48
49
50
# File 'lib/terminalwire/cache.rb', line 48

def value
  @value
end

Class Method Details

.key_path(value) ⇒ Object



44
45
46
# File 'lib/terminalwire/cache.rb', line 44

def self.key_path(value)
  Base64.urlsafe_encode64(value)
end

Instance Method Details

#deserializeObject



93
94
95
96
97
98
99
# File 'lib/terminalwire/cache.rb', line 93

def deserialize
  case MessagePack.unpack(File.open(@path, "rb", &:read), symbolize_keys: true)
  in { value:, expires:, version: VERSION }
    @value = value
    @expires = Time.parse(expires).utc if expires
  end
end

#destroyObject



101
102
103
# File 'lib/terminalwire/cache.rb', line 101

def destroy
  File.delete(@path)
end

#evictObject



89
90
91
# File 'lib/terminalwire/cache.rb', line 89

def evict
  destroy if expired?
end

#expired?(time: Time.now) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/terminalwire/cache.rb', line 67

def expired?(time: Time.now)
  @expires && @expires < time.utc
end

#fresh?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/terminalwire/cache.rb', line 71

def fresh?(...)
  not expired?(...)
end

#hit?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/terminalwire/cache.rb', line 75

def hit?
  persisted? and fresh?
end

#miss?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/terminalwire/cache.rb', line 79

def miss?
  not hit?
end

#nil?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/terminalwire/cache.rb', line 55

def nil?
  @value.nil?
end

#persisted?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/terminalwire/cache.rb', line 63

def persisted?
  File.exist? @path
end

#present?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/terminalwire/cache.rb', line 59

def present?
  not nil?
end

#saveObject



83
84
85
86
87
# File 'lib/terminalwire/cache.rb', line 83

def save
  File.open(@path, "wb") do |file| # Write in binary mode
    file.write(serialize)
  end
end