Class: RubyAPI::Plugins::Cache

Inherits:
RubyAPI::Plugin show all
Defined in:
lib/fastrb/plugins/cache.rb

Class Method Summary collapse

Methods inherited from RubyAPI::Plugin

inherited, option, options, plugin_name, register_cli, register_routes

Class Method Details

.clearObject



30
31
32
# File 'lib/fastrb/plugins/cache.rb', line 30

def self.clear
  store.clear
end

.delete(key) ⇒ Object



26
27
28
# File 'lib/fastrb/plugins/cache.rb', line 26

def self.delete(key)
  store.delete(key)
end

.get(key) ⇒ Object



11
12
13
14
15
16
# File 'lib/fastrb/plugins/cache.rb', line 11

def self.get(key)
  entry = store[key]
  return nil unless entry
  return nil if Time.now > entry[:expires_at]
  entry[:value]
end

.on_load(app) ⇒ Object



34
35
36
37
38
# File 'lib/fastrb/plugins/cache.rb', line 34

def self.on_load(app)
  app.before do |ctx|
    ctx.set(:cache, self)
  end
end

.set(key, value, ttl: nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/fastrb/plugins/cache.rb', line 18

def self.set(key, value, ttl: nil)
  ttl ||= options[:default_ttl]
  store[key] = {
    value: value,
    expires_at: Time.now + ttl
  }
end

.storeObject



7
8
9
# File 'lib/fastrb/plugins/cache.rb', line 7

def self.store
  options[:store]
end