Class: Sastrawi::Stemmer::Cache::ArrayCache
- Inherits:
-
Object
- Object
- Sastrawi::Stemmer::Cache::ArrayCache
- Defined in:
- lib/sastrawi/stemmer/cache/array_cache.rb
Constant Summary collapse
- DEFAULT_MAX_SIZE =
10_000
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
- #clear! ⇒ Object
- #data ⇒ Object
- #get(key) ⇒ Object
- #has?(key) ⇒ Boolean
-
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ ArrayCache
constructor
A new instance of ArrayCache.
- #set(key, value) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ ArrayCache
Returns a new instance of ArrayCache.
11 12 13 14 15 |
# File 'lib/sastrawi/stemmer/cache/array_cache.rb', line 11 def initialize(max_size: DEFAULT_MAX_SIZE) @data = {} @mutex = Mutex.new @max_size = max_size end |
Instance Attribute Details
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
9 10 11 |
# File 'lib/sastrawi/stemmer/cache/array_cache.rb', line 9 def max_size @max_size end |
Instance Method Details
#clear! ⇒ Object
44 45 46 |
# File 'lib/sastrawi/stemmer/cache/array_cache.rb', line 44 def clear! @mutex.synchronize { @data.clear } end |
#data ⇒ Object
17 18 19 |
# File 'lib/sastrawi/stemmer/cache/array_cache.rb', line 17 def data @mutex.synchronize { @data.dup } end |
#get(key) ⇒ Object
28 29 30 31 32 |
# File 'lib/sastrawi/stemmer/cache/array_cache.rb', line 28 def get(key) @mutex.synchronize do @data[key.to_sym] if @data.key?(key.to_sym) end end |
#has?(key) ⇒ Boolean
34 35 36 37 38 |
# File 'lib/sastrawi/stemmer/cache/array_cache.rb', line 34 def has?(key) @mutex.synchronize do @data.key?(key.to_sym) end end |
#set(key, value) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/sastrawi/stemmer/cache/array_cache.rb', line 21 def set(key, value) @mutex.synchronize do evict_if_full @data[key.to_sym] = value end end |
#size ⇒ Object
40 41 42 |
# File 'lib/sastrawi/stemmer/cache/array_cache.rb', line 40 def size @mutex.synchronize { @data.size } end |