Class: ThreadSafe::NonConcurrentCacheBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/thread_safe/non_concurrent_cache_backend.rb

Direct Known Subclasses

MriCacheBackend, SynchronizedCacheBackend

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ NonConcurrentCacheBackend

WARNING: all public methods of the class must operate on the @backend directly without calling each other. This is important because of the SynchronizedCacheBackend which uses a non-reentrant mutex for perfomance reasons.



7
8
9
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 7

def initialize(options = nil)
  @backend = {}
end

Instance Method Details

#[](key) ⇒ Object Also known as: _get



11
12
13
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 11

def [](key)
  @backend[key]
end

#[]=(key, value) ⇒ Object Also known as: _set



15
16
17
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 15

def []=(key, value)
  @backend[key] = value
end

#clearObject



88
89
90
91
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 88

def clear
  @backend.clear
  self
end

#compute(key) ⇒ Object



49
50
51
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 49

def compute(key)
  store_computed_value(key, yield(@backend[key]))
end

#compute_if_absent(key) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 19

def compute_if_absent(key)
  if NULL != (stored_value = @backend.fetch(key, NULL))
    stored_value
  else
    @backend[key] = yield
  end
end

#compute_if_present(key) ⇒ Object



43
44
45
46
47
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 43

def compute_if_present(key)
  if NULL != (stored_value = @backend.fetch(key, NULL))
    store_computed_value(key, yield(stored_value))
  end
end

#delete(key) ⇒ Object



75
76
77
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 75

def delete(key)
  @backend.delete(key)
end

#delete_pair(key, value) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 79

def delete_pair(key, value)
  if pair?(key, value)
    @backend.delete(key)
    true
  else
    false
  end
end

#each_pairObject



93
94
95
96
97
98
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 93

def each_pair
  dupped_backend.each_pair do |k, v|
    yield k, v
  end
  self
end

#get_and_set(key, value) ⇒ Object



61
62
63
64
65
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 61

def get_and_set(key, value)
  stored_value = @backend[key]
  @backend[key] = value
  stored_value
end

#get_or_default(key, default_value) ⇒ Object



104
105
106
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 104

def get_or_default(key, default_value)
  @backend.fetch(key, default_value)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 67

def key?(key)
  @backend.key?(key)
end

#merge_pair(key, value) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 53

def merge_pair(key, value)
  if NULL == (stored_value = @backend.fetch(key, NULL))
    @backend[key] = value
  else
    store_computed_value(key, yield(stored_value))
  end
end

#replace_if_exists(key, new_value) ⇒ Object



36
37
38
39
40
41
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 36

def replace_if_exists(key, new_value)
  if NULL != (stored_value = @backend.fetch(key, NULL))
    @backend[key] = new_value
    stored_value
  end
end

#replace_pair(key, old_value, new_value) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 27

def replace_pair(key, old_value, new_value)
  if pair?(key, old_value)
    @backend[key] = new_value
    true
  else
    false
  end
end

#sizeObject



100
101
102
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 100

def size
  @backend.size
end

#value?(value) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 71

def value?(value)
  @backend.value?(value)
end