Class: Racket::Utils::Views::TemplateCache
- Inherits:
-
Object
- Object
- Racket::Utils::Views::TemplateCache
- Defined in:
- lib/racket/utils/views/template_cache.rb
Overview
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.
Constant Summary collapse
- DEFAULT_OPTIONS =
Default options for template cache
{ expires: 0 }.freeze
Class Method Summary collapse
-
.service(_options = {}) ⇒ Proc
Returns a service proc that can be used by the registry.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #clear(_options = {}) ⇒ Object
- #close ⇒ Object
- #create(_key, _value, _options = {}) ⇒ Object
- #decrement(_key, _amount = 1, _options = {}) ⇒ Object
- #delete(key, _options = {}) ⇒ Object
- #features ⇒ Object
-
#fetch(*args) ⇒ Object
This method handles both forms of fetch.
- #increment(_key, _amount = 1, _options = {}) ⇒ Object
-
#initialize(options) ⇒ TemplateCache
constructor
A new instance of TemplateCache.
- #key?(key) ⇒ Boolean
- #load(key, _options = {}) ⇒ Object
- #store(key, value, options = {}) ⇒ Object
- #supports?(feature) ⇒ Boolean
Constructor Details
#initialize(options) ⇒ TemplateCache
Returns a new instance of TemplateCache.
38 39 40 41 42 |
# File 'lib/racket/utils/views/template_cache.rb', line 38 def initialize() @expirations = {} @items = {} @options = DEFAULT_OPTIONS.merge() end |
Class Method Details
.service(_options = {}) ⇒ Proc
Returns a service proc that can be used by the registry.
34 35 36 |
# File 'lib/racket/utils/views/template_cache.rb', line 34 def self.service( = {}) -> { new({}) } end |
Instance Method Details
#[](key) ⇒ Object
44 45 46 |
# File 'lib/racket/utils/views/template_cache.rb', line 44 def [](key) load(key) end |
#[]=(key, value) ⇒ Object
48 49 50 |
# File 'lib/racket/utils/views/template_cache.rb', line 48 def []=(key, value) store(key, value) end |
#clear(_options = {}) ⇒ Object
52 53 54 55 |
# File 'lib/racket/utils/views/template_cache.rb', line 52 def clear( = {}) @expirations.clear @items.clear end |
#close ⇒ Object
57 58 59 |
# File 'lib/racket/utils/views/template_cache.rb', line 57 def close clear end |
#create(_key, _value, _options = {}) ⇒ Object
61 62 63 |
# File 'lib/racket/utils/views/template_cache.rb', line 61 def create(_key, _value, = {}) raise NotImplementedError end |
#decrement(_key, _amount = 1, _options = {}) ⇒ Object
65 66 67 |
# File 'lib/racket/utils/views/template_cache.rb', line 65 def decrement(_key, _amount = 1, = {}) raise NotImplementedError end |
#delete(key, _options = {}) ⇒ Object
69 70 71 72 |
# File 'lib/racket/utils/views/template_cache.rb', line 69 def delete(key, = {}) @expirations.delete(key) @items.delete(key) end |
#features ⇒ Object
74 75 76 |
# File 'lib/racket/utils/views/template_cache.rb', line 74 def features [] end |
#fetch(*args) ⇒ Object
This method handles both forms of fetch. With a default block - fetch(key, options = {}, &block) With a default value - fetch(key, value, options = {})
81 82 83 84 85 |
# 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 = {}) ⇒ Object
87 88 89 |
# File 'lib/racket/utils/views/template_cache.rb', line 87 def increment(_key, _amount = 1, = {}) raise NotImplementedError end |
#key?(key) ⇒ Boolean
91 92 93 |
# File 'lib/racket/utils/views/template_cache.rb', line 91 def key?(key) @items.key?(key) end |
#load(key, _options = {}) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/racket/utils/views/template_cache.rb', line 95 def load(key, = {}) 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 = {}) ⇒ Object
104 105 106 107 |
# File 'lib/racket/utils/views/template_cache.rb', line 104 def store(key, value, = {}) set_expiration(key, .fetch(:expires, @options[:expires])) @items[key] = value end |
#supports?(feature) ⇒ Boolean
109 110 111 |
# File 'lib/racket/utils/views/template_cache.rb', line 109 def supports?(feature) features.include?(feature) end |