Class: SplitIoClient::Cache::Adapters::MemoryAdapters::MapAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb

Overview

Memory adapter implementation, which stores everything inside thread-safe Map

Instance Method Summary collapse

Constructor Details

#initializeMapAdapter

Returns a new instance of MapAdapter.



9
10
11
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 9

def initialize
  @map = Concurrent::Map.new
end

Instance Method Details

#add_to_map(key, field, value) ⇒ Object



22
23
24
25
26
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 22

def add_to_map(key, field, value)
  initialize_map(key) unless @map[key]

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

#add_to_set(key, values) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 91

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

#bool(key) ⇒ Object



80
81
82
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 80

def bool(key)
  @map[key]
end

#clear(_ = nil) ⇒ Object



13
14
15
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 13

def clear(_ = nil)
  initialize
end

#delete(key) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 116

def delete(key)
  if key.is_a? Array
    key.each { |k| @map.delete(k) }
  else
    @map.delete(key)
  end
end

#delete_from_map(key, fields) ⇒ Object Also known as: delete_from_set



34
35
36
37
38
39
40
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 34

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

#empty?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 124

def empty?
  @map.empty?
end

#exists?(key) ⇒ Boolean

General

Returns:

  • (Boolean)


112
113
114
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 112

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

#find_in_map(key, field) ⇒ Object



28
29
30
31
32
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 28

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

  @map[key].get(field)
end

#find_strings_by_prefix(prefix) ⇒ Object Also known as: find_sets_by_prefix



65
66
67
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 65

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

#get_all_from_set(key) ⇒ Object



99
100
101
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 99

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

#get_map(key) ⇒ Object



52
53
54
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 52

def get_map(key)
  @map[key]
end

#in_map?(key, field) ⇒ Boolean Also known as: in_set?

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 42

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

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

#initialize_map(key) ⇒ Object Also known as: initialize_set

Map



18
19
20
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 18

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

#map_keys(key) ⇒ Object Also known as: get_set



48
49
50
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 48

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

#multiple_strings(keys) ⇒ Object



69
70
71
72
73
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 69

def multiple_strings(keys)
  keys.each_with_object({}) do |key, memo|
    memo[key] = string(key)
  end
end

#pipelined(&block) ⇒ Object

This method is used in Redis adapter “stubbing” it here to keep the interface



130
131
132
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 130

def pipelined(&block)
  block.call
end

#set_bool(key, val) ⇒ Object

Bool



76
77
78
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 76

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

#set_string(key, str) ⇒ Object



61
62
63
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 61

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

#string(key) ⇒ Object

String



57
58
59
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 57

def string(key)
  @map[key]
end

#union_sets(set_keys) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/splitclient-rb/cache/adapters/memory_adapters/map_adapter.rb', line 103

def union_sets(set_keys)
  array = set_keys.each_with_object([]) do |key, memo|
    memo << get_set(key)
  end.flatten

  Set.new(array)
end