class Racket::Utils::Views::TemplateCache

Class for caching templates. This class adheres to the Moneta API (github.com/minad/moneta#user-content-moneta-api), even though it is not using the Moneta framework.

Constants

DEFAULT_OPTIONS

Default options for template cache

Public Class Methods

new(options) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 38
def initialize(options)
  @expirations = {}
  @items = {}
  @options = DEFAULT_OPTIONS.merge(options)
end
service(_options = {}) click to toggle source

Returns a service proc that can be used by the registry.

@param [Hash] _options (unused) @return [Proc]

# File lib/racket/utils/views/template_cache.rb, line 34
def self.service(_options = {})
  -> { new({}) }
end

Public Instance Methods

[](key) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 44
def [](key)
  load(key)
end
[]=(key, value) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 48
def []=(key, value)
  store(key, value)
end
clear(_options = {}) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 52
def clear(_options = {})
  @expirations.clear
  @items.clear
end
close() click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 57
def close
  clear
end
create(_key, _value, _options = {}) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 61
def create(_key, _value, _options = {})
  raise NotImplementedError
end
decrement(_key, _amount = 1, _options = {}) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 65
def decrement(_key, _amount = 1, _options = {})
  raise NotImplementedError
end
delete(key, _options = {}) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 69
def delete(key, _options = {})
  @expirations.delete(key)
  @items.delete(key)
end
features() click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 74
def features
  []
end
fetch(*args) { |: first| ... } click to toggle source

This method handles both forms of fetch. With a default block - fetch(key, options = {}, &block) With a default value - fetch(key, value, options = {})

# File lib/racket/utils/views/template_cache.rb, line 81
def fetch(*args)
  key = args.shift
  return load(key) if key?(key)
  block_given? ? yield : args.first
end
increment(_key, _amount = 1, _options = {}) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 87
def increment(_key, _amount = 1, _options = {})
  raise NotImplementedError
end
key?(key) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 91
def key?(key)
  @items.key?(key)
end
load(key, _options = {}) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 95
def load(key, _options = {})
  return @items[key] unless @expirations.key?(key)
  if Time.now > @expirations[key]
    @expirations.delete(key)
    @items.delete(key)
  end
  @items[key]
end
store(key, value, options = {}) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 104
def store(key, value, options = {})
  set_expiration(key, options.fetch(:expires, @options[:expires]))
  @items[key] = value
end
supports?(feature) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 109
def supports?(feature)
  features.include?(feature)
end

Private Instance Methods

set_expiration(key, expires) click to toggle source
# File lib/racket/utils/views/template_cache.rb, line 115
def set_expiration(key, expires)
  expire_at = expires > 0 ? Time.now + expires : nil
  if expire_at
    @expirations[key] = expire_at
  else
    @expirations.delete(key)
  end
end