flipper 0.22.2 → 0.23.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +6 -1
  3. data/.github/workflows/examples.yml +1 -0
  4. data/.rspec +1 -0
  5. data/Changelog.md +18 -2
  6. data/Gemfile +2 -2
  7. data/README.md +15 -67
  8. data/docs/README.md +1 -0
  9. data/docs/images/banner.jpg +0 -0
  10. data/lib/flipper/adapters/dual_write.rb +9 -15
  11. data/lib/flipper/adapters/failover.rb +83 -0
  12. data/lib/flipper/adapters/http/client.rb +11 -1
  13. data/lib/flipper/adapters/http/error.rb +19 -1
  14. data/lib/flipper/adapters/memoizable.rb +8 -16
  15. data/lib/flipper/adapters/pstore.rb +2 -5
  16. data/lib/flipper/adapters/sync/interval_synchronizer.rb +1 -6
  17. data/lib/flipper/adapters/sync.rb +9 -15
  18. data/lib/flipper/instrumenters/memory.rb +6 -2
  19. data/lib/flipper/version.rb +1 -1
  20. data/spec/flipper/actor_spec.rb +0 -2
  21. data/spec/flipper/adapter_spec.rb +0 -2
  22. data/spec/flipper/adapters/dual_write_spec.rb +0 -2
  23. data/spec/flipper/adapters/failover_spec.rb +129 -0
  24. data/spec/flipper/adapters/http_spec.rb +39 -3
  25. data/spec/flipper/adapters/instrumented_spec.rb +0 -2
  26. data/spec/flipper/adapters/memoizable_spec.rb +0 -2
  27. data/spec/flipper/adapters/memory_spec.rb +0 -3
  28. data/spec/flipper/adapters/operation_logger_spec.rb +0 -2
  29. data/spec/flipper/adapters/pstore_spec.rb +0 -2
  30. data/spec/flipper/adapters/read_only_spec.rb +0 -1
  31. data/spec/flipper/adapters/sync/feature_synchronizer_spec.rb +0 -1
  32. data/spec/flipper/adapters/sync/interval_synchronizer_spec.rb +4 -5
  33. data/spec/flipper/adapters/sync/synchronizer_spec.rb +0 -1
  34. data/spec/flipper/adapters/sync_spec.rb +0 -2
  35. data/spec/flipper/configuration_spec.rb +0 -1
  36. data/spec/flipper/dsl_spec.rb +0 -1
  37. data/spec/flipper/feature_check_context_spec.rb +0 -2
  38. data/spec/flipper/feature_spec.rb +0 -1
  39. data/spec/flipper/gate_spec.rb +0 -2
  40. data/spec/flipper/gate_values_spec.rb +0 -1
  41. data/spec/flipper/gates/actor_spec.rb +0 -2
  42. data/spec/flipper/gates/boolean_spec.rb +0 -2
  43. data/spec/flipper/gates/group_spec.rb +0 -2
  44. data/spec/flipper/gates/percentage_of_actors_spec.rb +0 -2
  45. data/spec/flipper/gates/percentage_of_time_spec.rb +0 -2
  46. data/spec/flipper/identifier_spec.rb +0 -1
  47. data/spec/flipper/instrumentation/log_subscriber_spec.rb +0 -1
  48. data/spec/flipper/instrumentation/statsd_subscriber_spec.rb +0 -1
  49. data/spec/flipper/instrumenters/memory_spec.rb +18 -1
  50. data/spec/flipper/instrumenters/noop_spec.rb +14 -8
  51. data/spec/flipper/middleware/memoizer_spec.rb +0 -2
  52. data/spec/flipper/middleware/setup_env_spec.rb +0 -2
  53. data/spec/flipper/railtie_spec.rb +17 -19
  54. data/spec/flipper/registry_spec.rb +0 -1
  55. data/spec/flipper/typecast_spec.rb +0 -1
  56. data/spec/flipper/types/actor_spec.rb +0 -1
  57. data/spec/flipper/types/boolean_spec.rb +0 -1
  58. data/spec/flipper/types/group_spec.rb +0 -1
  59. data/spec/flipper/types/percentage_of_actors_spec.rb +0 -1
  60. data/spec/flipper/types/percentage_of_time_spec.rb +0 -1
  61. data/spec/flipper/types/percentage_spec.rb +0 -1
  62. data/spec/flipper_integration_spec.rb +0 -1
  63. data/spec/flipper_spec.rb +0 -1
  64. data/spec/{helper.rb → spec_helper.rb} +2 -1
  65. data/spec/support/spec_helpers.rb +2 -6
  66. metadata +10 -13
  67. data/docs/Adapters.md +0 -124
  68. data/docs/Caveats.md +0 -4
  69. data/docs/Gates.md +0 -167
  70. data/docs/Instrumentation.md +0 -27
  71. data/docs/Optimization.md +0 -137
  72. data/docs/api/README.md +0 -884
  73. data/docs/http/README.md +0 -36
  74. data/docs/read-only/README.md +0 -24
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flipper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.2
4
+ version: 0.23.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-06 00:00:00.000000000 Z
11
+ date: 2021-12-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -20,6 +20,7 @@ files:
20
20
  - ".codeclimate.yml"
21
21
  - ".github/workflows/ci.yml"
22
22
  - ".github/workflows/examples.yml"
23
+ - ".rspec"
23
24
  - CODE_OF_CONDUCT.md
24
25
  - Changelog.md
25
26
  - Dockerfile
@@ -28,16 +29,9 @@ files:
28
29
  - README.md
29
30
  - Rakefile
30
31
  - docker-compose.yml
31
- - docs/Adapters.md
32
- - docs/Caveats.md
33
32
  - docs/DockerCompose.md
34
- - docs/Gates.md
35
- - docs/Instrumentation.md
36
- - docs/Optimization.md
37
- - docs/api/README.md
38
- - docs/http/README.md
33
+ - docs/README.md
39
34
  - docs/images/banner.jpg
40
- - docs/read-only/README.md
41
35
  - examples/api/basic.ru
42
36
  - examples/api/custom_memoized.ru
43
37
  - examples/api/memoized.ru
@@ -62,6 +56,7 @@ files:
62
56
  - lib/flipper/actor.rb
63
57
  - lib/flipper/adapter.rb
64
58
  - lib/flipper/adapters/dual_write.rb
59
+ - lib/flipper/adapters/failover.rb
65
60
  - lib/flipper/adapters/http.rb
66
61
  - lib/flipper/adapters/http/client.rb
67
62
  - lib/flipper/adapters/http/error.rb
@@ -114,6 +109,7 @@ files:
114
109
  - spec/flipper/actor_spec.rb
115
110
  - spec/flipper/adapter_spec.rb
116
111
  - spec/flipper/adapters/dual_write_spec.rb
112
+ - spec/flipper/adapters/failover_spec.rb
117
113
  - spec/flipper/adapters/http_spec.rb
118
114
  - spec/flipper/adapters/instrumented_spec.rb
119
115
  - spec/flipper/adapters/memoizable_spec.rb
@@ -154,7 +150,7 @@ files:
154
150
  - spec/flipper/types/percentage_spec.rb
155
151
  - spec/flipper_integration_spec.rb
156
152
  - spec/flipper_spec.rb
157
- - spec/helper.rb
153
+ - spec/spec_helper.rb
158
154
  - spec/support/descriptions.yml
159
155
  - spec/support/fake_udp_socket.rb
160
156
  - spec/support/spec_helpers.rb
@@ -182,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
178
  - !ruby/object:Gem::Version
183
179
  version: '0'
184
180
  requirements: []
185
- rubygems_version: 3.0.3
181
+ rubygems_version: 3.1.2
186
182
  signing_key:
187
183
  specification_version: 4
188
184
  summary: Feature flipper for ANYTHING
@@ -191,6 +187,7 @@ test_files:
191
187
  - spec/flipper/actor_spec.rb
192
188
  - spec/flipper/adapter_spec.rb
193
189
  - spec/flipper/adapters/dual_write_spec.rb
190
+ - spec/flipper/adapters/failover_spec.rb
194
191
  - spec/flipper/adapters/http_spec.rb
195
192
  - spec/flipper/adapters/instrumented_spec.rb
196
193
  - spec/flipper/adapters/memoizable_spec.rb
@@ -231,7 +228,7 @@ test_files:
231
228
  - spec/flipper/types/percentage_spec.rb
232
229
  - spec/flipper_integration_spec.rb
233
230
  - spec/flipper_spec.rb
234
- - spec/helper.rb
231
+ - spec/spec_helper.rb
235
232
  - spec/support/descriptions.yml
236
233
  - spec/support/fake_udp_socket.rb
237
234
  - spec/support/spec_helpers.rb
data/docs/Adapters.md DELETED
@@ -1,124 +0,0 @@
1
- # Adapters
2
-
3
- I plan on supporting the adapters in the flipper repo. Other adapters are welcome, so please let me know if you create one.
4
-
5
- ## Officially Supported
6
-
7
- * [ActiveRecord adapter](https://github.com/jnunemaker/flipper/blob/master/docs/active_record) - Rails 5 and 6.
8
- * [Sequel adapter](https://github.com/jnunemaker/flipper/blob/master/docs/sequel)
9
- * [Redis adapter](https://github.com/jnunemaker/flipper/blob/master/docs/redis)
10
- * [Mongo adapter](https://github.com/jnunemaker/flipper/blob/master/docs/mongo)
11
- * [PStore adapter](https://github.com/jnunemaker/flipper/blob/master/lib/flipper/adapters/pstore.rb) – great for when a local file is enough
12
- * [Http adapter](https://github.com/jnunemaker/flipper/blob/master/docs/http) - great for using with `Flipper::Api`
13
- * [Moneta adapter](https://github.com/jnunemaker/flipper/blob/master/docs/moneta) - great for a variety of data stores
14
- * [ActiveSupportCacheStore adapter](https://github.com/jnunemaker/flipper/blob/master/docs/active_support_cache_store) - great for Rails caching
15
- * [Memory adapter](https://github.com/jnunemaker/flipper/blob/master/lib/flipper/adapters/memory.rb) – great for tests
16
- * [Read-only adapter](https://github.com/jnunemaker/flipper/blob/master/docs/read-only) - great for preventing writes from production console
17
- * [Rollout adapter](rollout/README.md) - great for switching from rollout to flipper
18
-
19
- ## Community Supported
20
-
21
- * [Active Record 3 adapter](https://github.com/blueboxjesse/flipper-activerecord)
22
- * [Consul adapter](https://github.com/gdavison/flipper-consul)
23
-
24
- ## Roll Your Own
25
-
26
- The basic API for an adapter is this:
27
-
28
- * `features` - Get the set of known features.
29
- * `add(feature)` - Add a feature to the set of known features.
30
- * `remove(feature)` - Remove a feature from the set of known features.
31
- * `clear(feature)` - Clear all gate values for a feature.
32
- * `get(feature)` - Get all gate values for a feature.
33
- * `enable(feature, gate, thing)` - Enable a gate for a thing.
34
- * `disable(feature, gate, thing)` - Disable a gate for a thing.
35
- * `get_multi(features)` - Get all gate values for several features at once. Implementation is optional. If none provided, default implementation performs N+1 `get` calls where N is the number of elements in the features parameter.
36
- * `get_all` - Get all gate values for all features at once. Implementation is optional. If none provided, default implementation performs two calls, one to `features` to get the names of all features and one to `get_multi` with the feature names from the first call.
37
-
38
- If you would like to make your own adapter, there are shared adapter specs (RSpec) and tests (MiniTest) that you can use to verify that you have everything working correctly.
39
-
40
- ### RSpec
41
- For example, here is what the in-memory adapter spec looks like:
42
-
43
- `spec/flipper/adapters/memory_spec.rb`
44
-
45
- ```ruby
46
- require 'helper'
47
-
48
- # The shared specs are included with the flipper gem so you can use them in
49
- # separate adapter specific gems.
50
- require 'flipper/spec/shared_adapter_specs'
51
-
52
- describe Flipper::Adapters::Memory do
53
-
54
- # an instance of the new adapter you are trying to create
55
- subject { described_class.new }
56
-
57
- # include the shared specs that the subject must pass
58
- it_should_behave_like 'a flipper adapter'
59
- end
60
- ```
61
-
62
- ### MiniTest
63
-
64
- Here is what an in-memory adapter MiniTest looks like:
65
-
66
- `test/adapters/memory_test.rb`
67
-
68
- ```ruby
69
- require 'test_helper'
70
-
71
- class MemoryTest < MiniTest::Test
72
- prepend SharedAdapterTests
73
-
74
- def setup
75
- # Any code here will run before each test
76
- @adapter = Flipper::Adapters::Memory.new
77
- end
78
-
79
- def teardown
80
- # Any code here will run after each test
81
- end
82
- end
83
- ```
84
- 1. Create a file under `test/adapters` that inherits from MiniTest::Test.
85
-
86
- 2. `prepend SharedAdapterTests`.
87
-
88
- 3. Initialize an instance variable `@adapter` referencing an instance of the adapter.
89
-
90
- 4. Add any code to run before each test in a `setup` method and any code to run after each test in a `teardown` method.
91
-
92
- A good place to start when creating your own adapter is to copy one of the adapters mentioned above and replace the client specific code with whatever client you are attempting to adapt.
93
-
94
- I would also recommend setting `fail_fast = true` in your RSpec configuration as that will just give you one failure at a time to work through. It is also handy to have the shared adapter spec file open.
95
-
96
- ## Swapping Adapters
97
-
98
- If you find yourself using one adapter and would like to swap to another, you can do that! Flipper adapters support importing another adapter's data. This will wipe the adapter you are wanting to swap to, if it isn't already clean, so please be careful.
99
-
100
- ```ruby
101
- # Say you are using redis...
102
- redis_adapter = Flipper::Adapters::Redis.new(Redis.new)
103
- redis_flipper = Flipper.new(redis_adapter)
104
-
105
- # And redis has some stuff enabled...
106
- redis_flipper.enable(:search)
107
- redis_flipper.enable_percentage_of_time(:verbose_logging, 5)
108
- redis_flipper.enable_percentage_of_actors(:new_feature, 5)
109
- redis_flipper.enable_actor(:issues, Flipper::Actor.new('1'))
110
- redis_flipper.enable_actor(:issues, Flipper::Actor.new('2'))
111
- redis_flipper.enable_group(:request_tracing, :staff)
112
-
113
- # And you would like to switch to active record...
114
- ar_adapter = Flipper::Adapters::ActiveRecord.new
115
- ar_flipper = Flipper.new(ar_adapter)
116
-
117
- # NOTE: This wipes active record clean and copies features/gates from redis into active record.
118
- ar_flipper.import(redis_flipper)
119
-
120
- # active record is now identical to redis.
121
- ar_flipper.features.each do |feature|
122
- pp feature: feature.key, values: feature.gate_values
123
- end
124
- ```
data/docs/Caveats.md DELETED
@@ -1,4 +0,0 @@
1
- # Caveats
2
-
3
- 1. The [individual actor gate](https://github.com/jnunemaker/flipper/blob/master/docs/Gates.md#2-individual-actor) is typically not designed for hundreds or thousands of actors to be enabled. This is an explicit choice to make it easier to batch load data from the adapters instead of performing individual checks for actors over and over. If you need to enable something for more than 100 individual actors, I would recommend using a [group](https://github.com/jnunemaker/flipper/blob/master/docs/Gates.md#5-group).
4
- 2. The `disable` method exists only to clear something that is enabled. If the thing you are disabling is not enabled, the disable is pointless. This means that if you enable one group an actor is in and disable another group, the feature will be enabled for the actor. ([related issue](https://github.com/jnunemaker/flipper/issues/71))
data/docs/Gates.md DELETED
@@ -1,167 +0,0 @@
1
- # Gates
2
-
3
- Out of the box several types of enabling are supported. They are checked in this order:
4
-
5
- ## 1. Boolean
6
-
7
- All on or all off. Think top level things like `:stats`, `:search`, `:logging`, etc. Also, an easy way to release a new feature as once a feature is boolean enabled it is on for every situation.
8
-
9
- ```ruby
10
- Flipper.enable :stats # turn on
11
- Flipper.disable :stats # turn off
12
- Flipper.enabled? :stats # check
13
- ```
14
-
15
- ## 2. Individual Actor
16
-
17
- Turn feature on for individual thing. Think enable feature for someone to test or for a buddy.
18
-
19
- ```ruby
20
- Flipper.enable_actor :stats, user
21
- Flipper.enabled? :stats, user # true
22
-
23
- Flipper.disable_actor :stats, user
24
- Flipper.enabled? :stats, user # false
25
-
26
- # you can enable anything, does not need to be user or person
27
- Flipper.enable_actor :search, organization
28
- Flipper.enabled? :search, organization
29
-
30
- # you can also save a reference to a specific feature
31
- feature = Flipper[:search]
32
-
33
- feature.enable_actor user
34
- feature.enabled? user # true
35
- feature.disable_actor user
36
- ```
37
-
38
- The only requirement for an individual actor is that it must have a unique `flipper_id`. Include the `Flipper::Identifier` module for a default implementation which combines the class name and `id` (e.g. `User;6`).
39
-
40
- ```ruby
41
- class User < Struct.new(:id)
42
- include Flipper::Identifier
43
- end
44
-
45
- User.new(5).flipper_id # => "User;5"
46
- ```
47
-
48
- You can also define your own implementation:
49
-
50
- ```
51
- class Organization < Struct.new(:uuid)
52
- def flipper_id
53
- uuid
54
- end
55
- end
56
-
57
- Organization.new("DEB3D850-39FB-444B-A1E9-404A990FDBE0").flipper_id
58
- # => "DEB3D850-39FB-444B-A1E9-404A990FDBE0"
59
- ```
60
-
61
- Just make sure each type of object has a unique `flipper_id`.
62
-
63
- ## 3. Percentage of Actors
64
-
65
- Turn this on for a percentage of actors (think user, member, account, group, whatever). Consistently on or off for this user as long as percentage increases. Think slow rollout of a new feature to a percentage of things.
66
-
67
- ```ruby
68
- # turn stats on for 10 percent of users in the system
69
- Flipper.enable :stats, Flipper.actors(10)
70
- # or
71
- Flipper.enable_percentage_of_actors :stats, 10
72
-
73
- # checks if actor's flipper_id is in the enabled percentage by hashing
74
- # user.flipper_id.to_s to ensure enabled distribution is smooth
75
- Flipper.enabled? :stats, user
76
-
77
- Flipper.disable_percentage_of_actors :search # sets to 0
78
- # or
79
- Flipper.disable :stats, Flipper.actors(0)
80
-
81
- # you can also save a reference to a specific feature
82
- feature = Flipper[:search]
83
- feature.enable_percentage_of_actors 10
84
- feature.enabled? user
85
- feature.disable_percentage_of_actors # sets to 0
86
- ```
87
-
88
- ## 4. Percentage of Time
89
-
90
- Turn this on for a percentage of time. Think load testing new features behind the scenes and such.
91
-
92
- ```ruby
93
- # Register a feature called logging and turn it on for 5 percent of the time.
94
- # This could be on during one request and off the next
95
- # could even be on first time in request and off second time
96
- Flipper.enable_percentage_of_time :logging, 5
97
-
98
- Flipper.enabled? :logging # this will return true 5% of the time.
99
-
100
- Flipper.disable_percentage_of_time :logging # sets to 0
101
-
102
- # you can also save a reference to a specific feature
103
- feature = Flipper[:search]
104
- feature.enable_percentage_of_time, 5
105
- feature.enabled?
106
- feature.disable_percentage_of_time
107
- ```
108
-
109
- Timeness is not a good idea for enabling new features in the UI. Most of the time you want a feature on or off for a user, but there are definitely times when I have found percentage of time to be very useful.
110
-
111
- ## 5. Group
112
-
113
- Turn on feature based on the return value of block. Super flexible way to turn on a feature for multiple things (users, people, accounts, etc.) as long as the thing returns true when passed to the block.
114
-
115
- ```ruby
116
- # this registers a group
117
- Flipper.register(:admins) do |actor|
118
- actor.respond_to?(:admin?) && actor.admin?
119
- end
120
-
121
- Flipper.enable_group :stats, :admins # This registers a stats feature and turns it on for admins (which is anything that returns true from the registered block).
122
- Flipper.disable_group :stats, :admins # turn off the stats feature for admins
123
-
124
- person = Person.find(params[:id])
125
- Flipper.enabled? :stats, person # check if enabled, returns true if person.admin? is true
126
-
127
-
128
- # you can also use shortcut methods. This also registers a stats feature and turns it on for admins.
129
- feature = Flipper[:search]
130
- feature.enable_group :admins
131
- feature.enabled? person
132
- feature.disable_group :admins
133
- ```
134
-
135
- Here's a quick explanation of the above code block:
136
-
137
- ```ruby
138
- Flipper.register(:admins) do |actor|
139
- actor.respond_to?(:admin?) && actor.admin?
140
- end
141
- ```
142
- - The above first registers a group called `admins` which essentially saves a [Proc](http://www.eriktrautman.com/posts/ruby-explained-blocks-procs-and-lambdas-aka-closures) to be called later. The `actor` is an instance of the `Flipper::Types::Actor` that wraps the thing being checked against and `actor.thing` is the original object being checked.
143
-
144
- ```ruby
145
- Flipper.enable_group :stats, :admins
146
- ```
147
-
148
- - The above enables the stats feature to any object that returns true from the `:admins` proc.
149
-
150
- ```ruby
151
- person = Person.find(params[:id])
152
- Flipper.enabled? :stats, person # check if person is enabled, returns true if person.admin? is true
153
- ```
154
-
155
- When the `person` object is passed to the `enabled?` method, it is then passed into the proc. If the proc returns true, the entire statement returns true and so `Flipper[:stats].enabled? person` returns true. Whatever logic follows this conditional check is then executed.
156
-
157
- There is no requirement that the thing yielded to the block be a user model or whatever. It can be anything you want, therefore it is a good idea to check that the thing passed into the group block actually responds to what you are trying to do in the `register` proc.
158
-
159
- In your application code, you can do something like this now:
160
-
161
- ```ruby
162
- if Flipper.enabled? :stats, some_admin
163
- # do thing...
164
- else
165
- # do not do thing
166
- end
167
- ```
@@ -1,27 +0,0 @@
1
- # Instrumentation
2
-
3
- Flipper comes with automatic instrumentation. By default these work with ActiveSupport::Notifications, but only require the pieces of ActiveSupport that are needed and only do so if you actually attempt to require the instrumentation files listed below.
4
-
5
- To use the log subscriber:
6
-
7
- ```ruby
8
- # Gemfile
9
- gem "activesupport"
10
-
11
- # config/initializers/flipper.rb (or wherever you want it)
12
- require "flipper/instrumentation/log_subscriber"
13
- ```
14
-
15
- To use the statsd instrumentation:
16
-
17
- ```ruby
18
- # Gemfile
19
- gem "activesupport"
20
- gem "statsd-ruby"
21
-
22
- # config/initializers/flipper.rb (or wherever you want it)
23
- require "flipper/instrumentation/statsd"
24
- Flipper::Instrumentation::StatsdSubscriber.client = Statsd.new # or whatever your statsd instance is
25
- ```
26
-
27
- You can also do whatever you want with the instrumented events. Check out [this example](https://github.com/jnunemaker/flipper/blob/master/examples/instrumentation.rb) for more.
data/docs/Optimization.md DELETED
@@ -1,137 +0,0 @@
1
- # Optimization
2
-
3
- ## Memoization
4
-
5
- By default, Flipper will preload and memoize all features to ensure one adapter call per request. This means no matter how many times you check features, Flipper will only make one network request to Postgres, MySQL, Redis, Mongo or whatever adapter you are using for the length of the request.
6
-
7
- ### Preloading
8
-
9
- Flipper will preload all features before each request by default, which is recommended if you have a limited number of features (< 100?) and they are used on most requests. If you have a lot of features, but only a few are used on most requests, you may want to customize preloading:
10
-
11
- ```ruby
12
- # config/initializers/flipper.rb
13
- Rails.application.configure do
14
- # Load specific features that are used on most requests
15
- config.flipper.preload = [:stats, :search, :some_feature]
16
-
17
- # Or completely disable preloading
18
- config.flipper.preload = false
19
- end
20
- ```
21
-
22
- Features that are not preloaded are still memoized, ensuring one adapter call per feature during a request.
23
-
24
- ### Skip memoization
25
-
26
- Prevent preloading and memoization on specific requests by setting `memoize` to a proc that evaluates to false.
27
-
28
- ```ruby
29
- # config/initializers/flipper.rb
30
- Rails.application.configure do
31
- config.flipper.memoize = ->(request) { !request.path.start_with?("/assets") }
32
- end
33
- ```
34
-
35
- ### Disable memoization
36
-
37
- To disable memoization entirely:
38
-
39
- ```ruby
40
- Rails.application.configure do
41
- config.flipper.memoize = false
42
- end
43
- ```
44
-
45
- ### Advanced
46
-
47
- Memoization is implemented as a Rack middleware, which can be used manually in any Ruby app:
48
-
49
- ```ruby
50
- use Flipper::Middleware::Memoizer,
51
- preload: true,
52
- unless: ->(request) { request.path.start_with?("/assets") }
53
- ```
54
-
55
- **Also Note**: If you need to customize the instance of Flipper used by the memoizer, you can pass the instance to `SetupEnv`:
56
-
57
- ```ruby
58
- use Flipper::Middleware::SetupEnv, -> { Flipper.new(...) }
59
- use Flipper::Middleware::Memoizer
60
- ```
61
-
62
- ## Cache Adapters
63
-
64
- Cache adapters allow you to cache adapter calls for longer than a single request and should be used alongside the memoization middleware to add another caching layer.
65
-
66
- ### Dalli
67
-
68
- > Dalli is a high performance pure Ruby client for accessing memcached servers.
69
-
70
- https://github.com/petergoldstein/dalli
71
-
72
- Example using the Dalli cache adapter with the Memory adapter and a TTL of 600 seconds:
73
-
74
- ```ruby
75
- Flipper.configure do |config|
76
- config.adapter do
77
- dalli = Dalli::Client.new('localhost:11211')
78
- adapter = Flipper::Adapters::Memory.new
79
- Flipper::Adapters::Dalli.new(adapter, dalli, 600)
80
- end
81
- end
82
- ```
83
-
84
- ### RedisCache
85
-
86
- Applications using [Redis](https://redis.io/) via the [redis-rb](https://github.com/redis/redis-rb) client can take advantage of the RedisCache adapter.
87
-
88
- Initialize `RedisCache` with a flipper [adapter](https://github.com/jnunemaker/flipper/blob/master/docs/Adapters.md), a Redis client instance, and an optional TTL in seconds. TTL defaults to 3600 seconds.
89
-
90
- Example using the RedisCache adapter with the Memory adapter and a TTL of 4800 seconds:
91
-
92
- ```ruby
93
- require 'flipper/adapters/redis_cache'
94
-
95
- Flipper.configure do |config|
96
- config.adapter do
97
- redis = Redis.new(url: ENV['REDIS_URL'])
98
- memory_adapter = Flipper::Adapters::Memory.new
99
- Flipper::Adapters::RedisCache.new(memory_adapter, redis, 4800)
100
- end
101
- end
102
- ```
103
-
104
- ### ActiveSupportCacheStore
105
-
106
- Rails applications can cache Flipper calls in any [ActiveSupport::Cache::Store](http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html) implementation.
107
-
108
- Add this line to your application's Gemfile:
109
-
110
- gem 'flipper-active_support_cache_store'
111
-
112
- And then execute:
113
-
114
- $ bundle
115
-
116
- Or install it yourself with:
117
-
118
- $ gem install flipper-active_support_cache_store
119
-
120
- Example using the ActiveSupportCacheStore adapter with ActiveSupport's [MemoryStore](http://api.rubyonrails.org/classes/ActiveSupport/Cache/MemoryStore.html), Flipper's [Memory adapter](https://github.com/jnunemaker/flipper/blob/master/lib/flipper/adapters/memory.rb), and a TTL of 5 minutes.
121
-
122
- ```ruby
123
- require 'active_support/cache'
124
- require 'flipper/adapters/active_support_cache_store'
125
-
126
- Flipper.configure do |config|
127
- config.adapter do
128
- Flipper::Adapters::ActiveSupportCacheStore.new(
129
- Flipper::Adapters::Memory.new,
130
- ActiveSupport::Cache::MemoryStore.new # Or Rails.cache,
131
- expires_in: 5.minutes
132
- )
133
- end
134
- end
135
- ```
136
-
137
- Setting `expires_in` is optional and will set an expiration time on Flipper cache keys. If specified, all flipper keys will use this `expires_in` over the `expires_in` passed to your ActiveSupport cache constructor.