Sha256: a15f1fac33fde61c591859d95400bde1fce6d7ea7e6f428c8b7ecdd5bc44da80

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'set'

module Flipper
  module Adapters
    class Memoized
      # Public
      def initialize(adapter, cache = {})
        @adapter = adapter
        @cache = cache
      end

      # Public
      def read(key)
        @cache.fetch(key) {
          @cache[key] = @adapter.read(key)
        }
      end

      # Public
      def write(key, value)
        result = @adapter.write(key, value)
        @cache.delete(key)
        result
      end

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

      # Public
      def set_add(key, value)
        result = @adapter.set_add(key, value)
        @cache.delete(key)
        result
      end

      # Public
      def set_delete(key, value)
        result = @adapter.set_delete(key, value)
        @cache.delete(key)
        result
      end

      # Public
      def set_members(key)
        @cache.fetch(key) {
          @cache[key] = @adapter.set_members(key)
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flipper-0.4.0 lib/flipper/adapters/memoized.rb