Sha256: b1fef8d7ec2e18adf242b6fcee8f8e4e8249541c76c71dcfc9a7d1b5e56daf68

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

require 'concurrent'

module SplitIoClient
  module Cache
    module Adapters
      class MemoryAdapter
        def initialize
          @map = Concurrent::Map.new
        end

        # Map
        def initialize_map(key)
          @map[key] = Concurrent::Map.new
        end

        def add_to_map(key, field, value)
          @map[key].put(field, value)
        end

        def find_in_map(key, field)
          return nil if @map[key].nil?

          @map[key].get(field)
        end

        def delete_from_map(key, fields)
          if fields.is_a? Array
            fields.each { |field| @map[key].delete(field) }
          else
            @map[key].delete(field)
          end
        end

        def in_map?(key, field)
          return false if @map[key].nil?

          @map[key].key?(field)
        end

        def map_keys(key)
          @map[key].keys
        end

        def get_map(key)
          @map[key]
        end

        # String
        def string(key)
          @map[key]
        end

        def set_string(key, str)
          @map[key] = str
        end

        def find_strings_by_prefix(prefix)
          @map.keys.select { |str| str.start_with? prefix }
        end

        # Bool
        def set_bool(key, val)
          @map[key] = val
        end

        def bool(key)
          @map[key]
        end

        # Set
        alias_method :initialize_set, :initialize_map
        alias_method :get_set, :map_keys
        alias_method :delete_from_set, :delete_from_map
        alias_method :in_set?, :in_map?

        def add_to_set(key, values)
          if values.is_a? Array
            values.each { |value| add_to_map(key, value, 1) }
          else
            add_to_map(key, values, 1)
          end
        end

        # General
        def exists?(key)
          !@map[key].nil?
        end

        def delete(key)
          @map[key] = nil
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
splitclient-rb-3.1.0.pre.rc5 lib/cache/adapters/memory_adapter.rb
splitclient-rb-3.1.0.pre.rc4 lib/cache/adapters/memory_adapter.rb
splitclient-rb-3.1.0.pre.rc2 lib/cache/adapters/memory_adapter.rb