Class: Flipper::Adapter
- Inherits:
-
Object
- Object
- Flipper::Adapter
- Defined in:
- lib/flipper/adapter.rb
Overview
Internal: Adapter wrapper that wraps vanilla adapter instances with local caching.
So what is this local cache crap?
The main goal of the local cache is to prevent multiple queries to an adapter for the same key for a given amount of time (per request, per background job, etc.).
To facilitate with this, there is an included local cache middleware that enables local caching for the length of a web request. The local cache is enabled and cleared before each request and cleared and reset to original value after each request.
Examples
To see an example adapter that this would wrap, checkout the [memory adapter included with flipper](github.com/jnunemaker/flipper/blob/master/lib/flipper/adapters/memory.rb).
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#local_cache ⇒ Object
readonly
Returns the value of attribute local_cache.
Class Method Summary collapse
-
.wrap(object) ⇒ Object
Internal: Wraps vanilla adapter instance for use internally in flipper.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(adapter, local_cache = {}) ⇒ Adapter
constructor
Internal: Initializes a new instance.
- #read(key) ⇒ Object
- #set_add(key, value) ⇒ Object
- #set_delete(key, value) ⇒ Object
- #set_members(key) ⇒ Object
- #use_local_cache=(value) ⇒ Object
- #using_local_cache? ⇒ Boolean
- #write(key, value) ⇒ Object
Constructor Details
#initialize(adapter, local_cache = {}) ⇒ Adapter
Internal: Initializes a new instance
adapter - Vanilla adapter instance to wrap. Just needs to respond to
read, write, delete, set_members, set_add, and set_delete.
local_cache - Where to store the local cache data (default: {}).
Must respond to fetch(key, block), delete(key) and clear.
53 54 55 56 |
# File 'lib/flipper/adapter.rb', line 53 def initialize(adapter, local_cache = {}) @adapter = adapter @local_cache = local_cache end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
44 45 46 |
# File 'lib/flipper/adapter.rb', line 44 def adapter @adapter end |
#local_cache ⇒ Object (readonly)
Returns the value of attribute local_cache.
44 45 46 |
# File 'lib/flipper/adapter.rb', line 44 def local_cache @local_cache end |
Class Method Details
.wrap(object) ⇒ Object
Internal: Wraps vanilla adapter instance for use internally in flipper.
object - Either an instance of Flipper::Adapter or a vanilla adapter instance
Examples
adapter = Flipper::Adapters::Memory.new
instance = Flipper::Adapter.new(adapter)
Flipper::Adapter.wrap(instance)
# => Flipper::Adapter instance
Flipper::Adapter.wrap(adapter)
# => Flipper::Adapter instance
Returns Flipper::Adapter instance
36 37 38 39 40 41 42 |
# File 'lib/flipper/adapter.rb', line 36 def self.wrap(object) if object.is_a?(Flipper::Adapter) object else new(object) end end |
Instance Method Details
#delete(key) ⇒ Object
79 80 81 |
# File 'lib/flipper/adapter.rb', line 79 def delete(key) @adapter.delete(key).tap { expire_local_cache(key) } end |
#eql?(other) ⇒ Boolean Also known as: ==
99 100 101 |
# File 'lib/flipper/adapter.rb', line 99 def eql?(other) self.class.eql?(other.class) && adapter == other.adapter end |
#read(key) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/flipper/adapter.rb', line 67 def read(key) if using_local_cache? cache(key) { @adapter.read(key) } else @adapter.read(key) end end |
#set_add(key, value) ⇒ Object
91 92 93 |
# File 'lib/flipper/adapter.rb', line 91 def set_add(key, value) @adapter.set_add(key, value).tap { expire_local_cache(key) } end |
#set_delete(key, value) ⇒ Object
95 96 97 |
# File 'lib/flipper/adapter.rb', line 95 def set_delete(key, value) @adapter.set_delete(key, value).tap { expire_local_cache(key) } end |
#set_members(key) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/flipper/adapter.rb', line 83 def set_members(key) if using_local_cache? cache(key) { @adapter.set_members(key) } else @adapter.set_members(key) end end |
#use_local_cache=(value) ⇒ Object
58 59 60 61 |
# File 'lib/flipper/adapter.rb', line 58 def use_local_cache=(value) local_cache.clear @use_local_cache = value end |
#using_local_cache? ⇒ Boolean
63 64 65 |
# File 'lib/flipper/adapter.rb', line 63 def using_local_cache? @use_local_cache == true end |
#write(key, value) ⇒ Object
75 76 77 |
# File 'lib/flipper/adapter.rb', line 75 def write(key, value) @adapter.write(key, value).tap { expire_local_cache(key) } end |