Class: Asynchronic::DataStore::InMemory

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/asynchronic/data_store/in_memory.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#clear, #each, #lazy, #lazy?, #merge, #no_lazy, #readonly, #readonly?, #scoped

Constructor Details

#initializeInMemory

Returns a new instance of InMemory.



15
16
17
18
19
20
# File 'lib/asynchronic/data_store/in_memory.rb', line 15

def initialize
  @hash = {}
  @mutex = Mutex.new
  @keys_mutex = {}
  self.class.connections[object_id] = self
end

Class Method Details

.connect(object_id) ⇒ Object



7
8
9
# File 'lib/asynchronic/data_store/in_memory.rb', line 7

def self.connect(object_id)
  connections[object_id]
end

.connectionsObject



11
12
13
# File 'lib/asynchronic/data_store/in_memory.rb', line 11

def self.connections
  @connections ||= {}
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/asynchronic/data_store/in_memory.rb', line 22

def [](key)
  Marshal.load(hash[key.to_s]) if hash.key? key.to_s
end

#[]=(key, value) ⇒ Object



26
27
28
# File 'lib/asynchronic/data_store/in_memory.rb', line 26

def []=(key, value)
  mutex.synchronize { hash[key.to_s] = Marshal.dump(value) }
end

#connection_argsObject



50
51
52
# File 'lib/asynchronic/data_store/in_memory.rb', line 50

def connection_args
  [object_id]
end

#delete(key) ⇒ Object



30
31
32
# File 'lib/asynchronic/data_store/in_memory.rb', line 30

def delete(key)
  hash.delete key.to_s
end

#delete_cascade(key) ⇒ Object



34
35
36
37
# File 'lib/asynchronic/data_store/in_memory.rb', line 34

def delete_cascade(key)
  keys.select { |k| k.sections.first == key }
      .each { |k| delete k }
end

#keysObject



39
40
41
# File 'lib/asynchronic/data_store/in_memory.rb', line 39

def keys
  hash.keys.map { |k| Key[k] }
end

#synchronize(key, &block) ⇒ Object



43
44
45
46
47
48
# File 'lib/asynchronic/data_store/in_memory.rb', line 43

def synchronize(key, &block)
  mutex.synchronize do
    keys_mutex[key] ||= Mutex.new
  end
  keys_mutex[key].synchronize(&block)
end