Class: ThreadSafe::MriCacheBackend

Inherits:
NonConcurrentCacheBackend show all
Defined in:
lib/thread_safe/mri_cache_backend.rb

Constant Summary collapse

WRITE_LOCK =

We can get away with a single global write lock (instead of a per-instance one) because of the GVL/green threads.

NOTE: a neat idea of writing a c-ext to manually perform atomic put_if_absent, while relying on Ruby not releasing a GVL while calling a c-ext will not work because of the potentially Ruby implemented #hash and #eql? key methods.

Mutex.new

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from ThreadSafe::NonConcurrentCacheBackend

Instance Method Details

#[]=(key, value) ⇒ Object



12
13
14
# File 'lib/thread_safe/mri_cache_backend.rb', line 12

def []=(key, value)
  WRITE_LOCK.synchronize { super }
end

#clearObject



56
57
58
# File 'lib/thread_safe/mri_cache_backend.rb', line 56

def clear
  WRITE_LOCK.synchronize { super }
end

#compute(key) ⇒ Object



28
29
30
# File 'lib/thread_safe/mri_cache_backend.rb', line 28

def compute(key)
  WRITE_LOCK.synchronize { super }
end

#compute_if_absent(key) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/thread_safe/mri_cache_backend.rb', line 16

def compute_if_absent(key)
  if stored_value = _get(key) # fast non-blocking path for the most likely case
    stored_value
  else
    WRITE_LOCK.synchronize { super }
  end
end

#compute_if_present(key) ⇒ Object



24
25
26
# File 'lib/thread_safe/mri_cache_backend.rb', line 24

def compute_if_present(key)
  WRITE_LOCK.synchronize { super }
end

#delete(key) ⇒ Object



48
49
50
# File 'lib/thread_safe/mri_cache_backend.rb', line 48

def delete(key)
  WRITE_LOCK.synchronize { super }
end

#delete_pair(key, value) ⇒ Object



52
53
54
# File 'lib/thread_safe/mri_cache_backend.rb', line 52

def delete_pair(key, value)
  WRITE_LOCK.synchronize { super }
end

#get_and_set(key, value) ⇒ Object



44
45
46
# File 'lib/thread_safe/mri_cache_backend.rb', line 44

def get_and_set(key, value)
  WRITE_LOCK.synchronize { super }
end

#merge_pair(key, value) ⇒ Object



32
33
34
# File 'lib/thread_safe/mri_cache_backend.rb', line 32

def merge_pair(key, value)
  WRITE_LOCK.synchronize { super }
end

#replace_if_exists(key, new_value) ⇒ Object



40
41
42
# File 'lib/thread_safe/mri_cache_backend.rb', line 40

def replace_if_exists(key, new_value)
  WRITE_LOCK.synchronize { super }
end

#replace_pair(key, old_value, new_value) ⇒ Object



36
37
38
# File 'lib/thread_safe/mri_cache_backend.rb', line 36

def replace_pair(key, old_value, new_value)
  WRITE_LOCK.synchronize { super }
end