Class: RequestLocals
- Inherits:
-
Object
- Object
- RequestLocals
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/request_locals.rb
Overview
Public: Provides per-request global storage, by offering an interface that is very similar to Rails.cache, or Hash.
The store may be shared between threads, as long as the :request_id thread-local variable is set.
Intended to work in collaboration with the RequestStoreRails middleware, that will clear the request local variables after each request.
Defined Under Namespace
Classes: Cache
Instance Method Summary collapse
-
#clear! ⇒ Object
Public: Removes all the request-local variables.
-
#clear_all! ⇒ Object
Public: Clears all the request-local variable stores.
-
#exist?(key) ⇒ Boolean
(also: #key?)
Public: Checks if a value was stored for the given key.
-
#fetch(key, &block) ⇒ Object
Public: Implements fetch in a consistent way with Rails.cache, persisting the value yielded by the block if the key was not found.
-
#initialize ⇒ RequestLocals
constructor
A new instance of RequestLocals.
Constructor Details
#initialize ⇒ RequestLocals
Returns a new instance of RequestLocals.
41 42 43 |
# File 'lib/request_locals.rb', line 41 def initialize @cache = Cache.new end |
Instance Method Details
#clear! ⇒ Object
Public: Removes all the request-local variables.
Returns nothing.
48 49 50 |
# File 'lib/request_locals.rb', line 48 def clear! @cache.delete(current_request_id) end |
#clear_all! ⇒ Object
Public: Clears all the request-local variable stores.
Returns nothing.
55 56 57 |
# File 'lib/request_locals.rb', line 55 def clear_all! @cache = Cache.new end |
#exist?(key) ⇒ Boolean Also known as: key?
Public: Checks if a value was stored for the given key.
Returns true if there is a value stored for the key.
62 63 64 |
# File 'lib/request_locals.rb', line 62 def exist?(key) store.key?(key) end |
#fetch(key, &block) ⇒ Object
Public: Implements fetch in a consistent way with Rails.cache, persisting the value yielded by the block if the key was not found.
Returns an existing value for the key is found, otherwise it returns the value yielded by the block.
74 75 76 |
# File 'lib/request_locals.rb', line 74 def fetch(key, &block) store.compute_if_absent(key, &block) end |