Sha256: e39644ce9e6720489c8df997f4a4ffdbcdf80e7186ed2bf02aaafebb3ecd805c

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 KB

Contents

module Flipflop
  class FeatureCache
    class Middleware
      def initialize(app)
        @app = app
      end

      def call(env)
        return @app.call(env) if FeatureCache.current.enabled?

        FeatureCache.current.enable!
        response = @app.call(env)
        response[2] = Rack::BodyProxy.new(response[2]) do
          FeatureCache.current.disable!
        end
        response
      rescue Exception => err
        FeatureCache.current.disable!
        raise err
      end
    end

    class << self
      def current
        Thread.current.thread_variable_get(:flipflop_cache) or
        Thread.current.thread_variable_set(:flipflop_cache, new)
      end

      private :new
    end

    def initialize
      @enabled = false
      @cache = {}
    end

    def enabled?
      @enabled
    end

    def clear!
      @cache.clear
    end

    def enable!
      @enabled = true
    end

    def disable!
      @enabled = false
      @cache.clear
    end

    def fetch(key)
      if @enabled
        @cache.fetch(key) do
          @cache[key] = yield
        end
      else
        yield
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
flipflop-2.8.0 lib/flipflop/feature_cache.rb
flipflop-2.7.1 lib/flipflop/feature_cache.rb
flipflop-2.7.0 lib/flipflop/feature_cache.rb
flipflop-2.6.0 lib/flipflop/feature_cache.rb
flipflop-2.5.0 lib/flipflop/feature_cache.rb
flipflop-2.4.0 lib/flipflop/feature_cache.rb
flipflop-2.3.1 lib/flipflop/feature_cache.rb
flipflop-2.3.0 lib/flipflop/feature_cache.rb
flipflop-2.2.1 lib/flipflop/feature_cache.rb
flipflop-2.2.0 lib/flipflop/feature_cache.rb
flipflop-2.1.0 lib/flipflop/feature_cache.rb
flipflop-2.0.0 lib/flipflop/feature_cache.rb