Class: Kotoshu::Cache::SuggestionCache
- Inherits:
-
LookupCache
- Object
- LookupCache
- Kotoshu::Cache::SuggestionCache
- Defined in:
- lib/kotoshu/cache/suggestion_cache.rb
Overview
LRU cache specifically for suggestion results.
Extends LookupCache with suggestion-specific features like caching by word + max_results combination.
Constant Summary collapse
- DEFAULT_MAX_SIZE =
Default maximum cache size for suggestions
5000
Instance Attribute Summary
Attributes inherited from LookupCache
Instance Method Summary collapse
-
#delete(word, max_results: 10) ⇒ Array<String>?
Delete suggestions from cache.
-
#fetch(word, max_results: 10) { ... } ⇒ Array<String>
Fetch suggestions from cache or compute them.
-
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ SuggestionCache
constructor
Create a new suggestion cache.
-
#invalidate_word(word) ⇒ self
Invalidate all cached suggestions for a word.
-
#key?(word, max_results: 10) ⇒ Boolean
Check if suggestions are cached for this word.
-
#read(word, max_results: 10) ⇒ Array<String>?
Read suggestions from cache.
-
#write(word, suggestions, max_results: 10) ⇒ Array<String>
Write suggestions to cache.
Methods inherited from LookupCache
#clear, #reset_stats, #size, #stats
Methods included from Cache
#clear, #reset_stats, #size, #stats
Constructor Details
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ SuggestionCache
Create a new suggestion cache.
23 24 25 |
# File 'lib/kotoshu/cache/suggestion_cache.rb', line 23 def initialize(max_size: DEFAULT_MAX_SIZE) super(max_size: max_size) end |
Instance Method Details
#delete(word, max_results: 10) ⇒ Array<String>?
Delete suggestions from cache.
75 76 77 78 |
# File 'lib/kotoshu/cache/suggestion_cache.rb', line 75 def delete(word, max_results: 10) cache_key = cache_key_for(word, max_results) super(cache_key) end |
#fetch(word, max_results: 10) { ... } ⇒ Array<String>
Fetch suggestions from cache or compute them.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/kotoshu/cache/suggestion_cache.rb', line 54 def fetch(word, max_results: 10) cache_key = cache_key_for(word, max_results) if @data.key?(cache_key) record_hit @access_order += 1 @data[cache_key][1] = @access_order # Update access order @data[cache_key][0] # Return value else record_miss suggestions = yield write(word, suggestions, max_results: max_results) suggestions end end |
#invalidate_word(word) ⇒ self
Invalidate all cached suggestions for a word.
94 95 96 97 98 99 |
# File 'lib/kotoshu/cache/suggestion_cache.rb', line 94 def invalidate_word(word) # Find and delete all cache entries for this word keys_to_delete = @data.keys.select { |key| key.start_with?("#{word}|") } keys_to_delete.each { |key| @data.delete(key) } self end |
#key?(word, max_results: 10) ⇒ Boolean
Check if suggestions are cached for this word.
85 86 87 88 |
# File 'lib/kotoshu/cache/suggestion_cache.rb', line 85 def key?(word, max_results: 10) cache_key = cache_key_for(word, max_results) super(cache_key) end |
#read(word, max_results: 10) ⇒ Array<String>?
Read suggestions from cache.
43 44 45 46 |
# File 'lib/kotoshu/cache/suggestion_cache.rb', line 43 def read(word, max_results: 10) cache_key = cache_key_for(word, max_results) super(cache_key) end |
#write(word, suggestions, max_results: 10) ⇒ Array<String>
Write suggestions to cache.
33 34 35 36 |
# File 'lib/kotoshu/cache/suggestion_cache.rb', line 33 def write(word, suggestions, max_results: 10) cache_key = cache_key_for(word, max_results) super(cache_key, suggestions) end |