Class: AtomicCache::Storage::SharedMemory

Inherits:
Memory
  • Object
show all
Defined in:
lib/atomic_cache/storage/shared_memory.rb

Overview

A storage adapter which keeps all values in memory, global to all threads

Constant Summary collapse

STORE =
{}
SEMAPHORE =
Mutex.new

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Memory

#delete, #read, #set

Methods inherited from Store

#delete, #read, #set

Class Attribute Details

.enforce_ttlObject

Returns the value of attribute enforce_ttl.



15
16
17
# File 'lib/atomic_cache/storage/shared_memory.rb', line 15

def enforce_ttl
  @enforce_ttl
end

Class Method Details

.resetObject



28
29
30
# File 'lib/atomic_cache/storage/shared_memory.rb', line 28

def self.reset
  STORE.clear
end

.storeObject



32
33
34
# File 'lib/atomic_cache/storage/shared_memory.rb', line 32

def self.store
  STORE
end

Instance Method Details

#add(raw_key, new_value, ttl, user_options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/atomic_cache/storage/shared_memory.rb', line 18

def add(raw_key, new_value, ttl, user_options={})
  if self.class.enforce_ttl
    super(raw_key, new_value, ttl, user_options)
  else
    store_op(raw_key, user_options) do |key, options|
      write(key, new_value, ttl, user_options)
    end
  end
end

#resetObject



36
37
38
# File 'lib/atomic_cache/storage/shared_memory.rb', line 36

def reset
  self.class.reset
end

#storeObject



40
41
42
# File 'lib/atomic_cache/storage/shared_memory.rb', line 40

def store
  STORE
end

#store_op(key, user_options = {}) ⇒ Object



44
45
46
47
48
49
# File 'lib/atomic_cache/storage/shared_memory.rb', line 44

def store_op(key, user_options={})
  normalized_key = key.to_sym
  SEMAPHORE.synchronize do
    yield(normalized_key, user_options)
  end
end