Class: Spider::Utils::MemorySharedStore

Inherits:
SharedStore show all
Defined in:
lib/spiderfw/utils/shared_store/memory_shared_store.rb

Overview

Implementation of the SharedStore in memory. This is a transient, per-process, thread safe store. (See SharedStore about thread safety).

Instance Method Summary collapse

Methods inherited from SharedStore

get, #lock_all

Constructor Details

#initialize(config = {}) ⇒ MemorySharedStore

Returns a new instance of MemorySharedStore.



9
10
11
12
13
# File 'lib/spiderfw/utils/shared_store/memory_shared_store.rb', line 9

def initialize(config={})
    super
    @data = {}
    @sync = Sync.new
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
18
19
20
# File 'lib/spiderfw/utils/shared_store/memory_shared_store.rb', line 15

def [](key)
    @sync.lock(:SH)
    res = @data[key]
    @sync.lock(:UN)
    return res
end

#[]=(key, value) ⇒ Object



22
23
24
25
26
# File 'lib/spiderfw/utils/shared_store/memory_shared_store.rb', line 22

def []=(key, value)
    @sync.lock(:EX)
    @data[key] = value
    @sync.lock(:UN)
end

#delete(key) ⇒ Object



28
29
30
31
32
# File 'lib/spiderfw/utils/shared_store/memory_shared_store.rb', line 28

def delete(key)
    @sync.lock(:EX)
    @data.delete(key)
    @sync.lock(:UN)
end

#each_keyObject



38
39
40
41
42
# File 'lib/spiderfw/utils/shared_store/memory_shared_store.rb', line 38

def each_key
    @data.keys.each do |k|
        yield k
    end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/spiderfw/utils/shared_store/memory_shared_store.rb', line 34

def include?(key)
    @data.include?(key)
end