Sha256: 5c993e70c449b476b8d580fda7c221b5c82cc291abb9582918dcbb3b66d92759

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

require 'dalli'

module Flipper
  module Adapters
    # Public: Adapter that wraps another adapter with the ability to cache
    # adapter calls in Memcached using the Dalli gem.
    class Dalli
      include ::Flipper::Adapter

      FeaturesKey = :flipper_features

      # Internal
      attr_reader :cache

      # Public: The name of the adapter.
      attr_reader :name

      # Public
      def initialize(adapter, cache, ttl = 0)
        @adapter = adapter
        @name = :dalli
        @cache = cache
        @ttl = ttl
      end

      # Public
      def features
        @cache.fetch(FeaturesKey, @ttl) do
          @adapter.features
        end
      end

      # Public
      def add(feature)
        result = @adapter.add(feature)
        @cache.delete(FeaturesKey)
        result
      end

      # Public
      def remove(feature)
        result = @adapter.remove(feature)
        @cache.delete(FeaturesKey)
        result
      end

      # Public
      def clear(feature)
        result = @adapter.clear(feature)
        @cache.delete(feature)
        result
      end

      # Public
      def get(feature)
        @cache.fetch(feature, @ttl) do
          @adapter.get(feature)
        end
      end

      # Public
      def enable(feature, gate, thing)
        result = @adapter.enable(feature, gate, thing)
        @cache.delete(feature)
        result
      end

      # Public
      def disable(feature, gate, thing)
        result = @adapter.disable(feature, gate, thing)
        @cache.delete(feature)
        result
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
flipper-dalli-0.10.1 lib/flipper/adapters/dalli.rb
flipper-dalli-0.10.0 lib/flipper/adapters/dalli.rb
flipper-dalli-0.9.2 lib/flipper/adapters/dalli.rb
flipper-dalli-0.9.1 lib/flipper/adapters/dalli.rb
flipper-dalli-0.9.0 lib/flipper/adapters/dalli.rb