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.


5
6
7
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 5

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

Instance Method Details

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


9
10
11
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 9

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

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


13
14
15
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 13

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

#clearObject


86
87
88
89
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 86

def clear
  @backend.clear
  self
end

#compute(key) ⇒ Object


47
48
49
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 47

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

#compute_if_absent(key) ⇒ Object


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

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


41
42
43
44
45
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 41

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


73
74
75
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 73

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

#delete_pair(key, value) ⇒ Object


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

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

#each_pairObject


91
92
93
94
95
96
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 91

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

#get_and_set(key, value) ⇒ Object


59
60
61
62
63
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 59

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

#get_or_default(key, default_value) ⇒ Object


102
103
104
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 102

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)

65
66
67
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 65

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

#merge_pair(key, value) ⇒ Object


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

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


34
35
36
37
38
39
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 34

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


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

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

#sizeObject


98
99
100
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 98

def size
  @backend.size
end

#value?(value) ⇒ Boolean

Returns:

  • (Boolean)

69
70
71
# File 'lib/thread_safe/non_concurrent_cache_backend.rb', line 69

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