docs/Optimization.md in flipper-0.11.0.beta7 vs docs/Optimization.md in flipper-0.11.0.beta8

- old
+ new

@@ -1,46 +1,37 @@ # Optimization ## Memoizing Middleware -One optimization that flipper provides is a memoizing middleware. The memoizing middleware ensures that you only make one adapter call per feature per request. +One optimization that flipper provides is a memoizing middleware. The memoizing middleware ensures that you only make one adapter call per feature per request. This means if you check the same feature over and over, it will only make one Mongo, Redis, or whatever call per feature for the length of the request. -This means if you check the same feature over and over, it will only make one Mongo, Redis, or whatever call per feature for the length of the request. +You can use the middleware like so for Rails: -You can use the middleware from a Rails initializer like so: - ```ruby -# create flipper dsl instance, see above Usage for more details -flipper = Flipper.new(...) +# setup default instance (perhaps in config/initializer/flipper.rb) +Flipper.configure do |config| + config.default do + Flipper.new(...) + end +end -require 'flipper/middleware/setup_env' +# This assumes you setup a default flipper instance using configure. require 'flipper/middleware/memoizer' -config.middleware.use Flipper::Middleware::SetupEnv, flipper config.middleware.use Flipper::Middleware::Memoizer ``` -If you set your flipper instance up in an initializer, you can pass a block to the middleware and it will lazily load the instance the first time the middleware is invoked. +**Note**: Be sure that the middleware is high enough up in your stack that all feature checks are wrapped. -```ruby -# config/initializers/flipper.rb -module MyRailsApp - def self.flipper - @flipper ||= Flipper.new(...) - end -end +**Also Note**: If you haven't setup a default instance, you can pass the instance to `SetupEnv` as `Memoizer` uses whatever is setup in the `env`: -# config/application.rb +```ruby require 'flipper/middleware/setup_env' require 'flipper/middleware/memoizer' -config.middleware.use Flipper::Middleware::SetupEnv, lambda { - MyRailsApp.flipper -} +config.middleware.use Flipper::Middleware::SetupEnv, -> { Flipper.new(...) } config.middleware.use Flipper::Middleware::Memoizer ``` -**Note**: Be sure that the middleware is high enough up in your stack that all feature checks are wrapped. - ### Options The Memoizer middleware also supports a few options. Use either `preload` or `preload_all`, not both. * **`:preload`** - An `Array` of feature names (`Symbol`) to preload for every request. Useful if you have features that are used on every endpoint. `preload` uses `Adapter#get_multi` to attempt to load the features in one network call instead of N+1 network calls. @@ -100,18 +91,18 @@ Rails applications can cache Flipper calls in any [ActiveSupport::Cache::Store](http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html) implementation. Add this line to your application's Gemfile: - gem 'flipper-cache-store' + gem 'flipper-cache_store' And then execute: $ bundle Or install it yourself with: - $ gem install flipper-cache-store + $ gem install flipper-cache_store Example using the CacheStore 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. ```ruby require 'active_support/cache'