Class: Kotoshu::Cache::SuggestionCache

Inherits:
LookupCache show all
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.

Examples:

Caching suggestions

cache = SuggestionCache.new(max_size: 5000)
cache.write("helo", ["hello", "help"], max_results: 10)
cache.read("helo", max_results: 10)  # => ["hello", "help"]

Constant Summary collapse

DEFAULT_MAX_SIZE =

Default maximum cache size for suggestions

5000

Instance Attribute Summary

Attributes inherited from LookupCache

#max_size

Instance Method Summary collapse

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.

Parameters:

  • max_size (Integer) (defaults to: DEFAULT_MAX_SIZE)

    Maximum number of entries (default: 5000)



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.

Parameters:

  • word (String)

    The misspelled word

  • max_results (Integer) (defaults to: 10)

    Max results for this query

Returns:

  • (Array<String>, nil)

    Deleted suggestions or nil



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.

Parameters:

  • word (String)

    The misspelled word

  • max_results (Integer) (defaults to: 10)

    Max results for this query

Yields:

  • Block to compute suggestions on cache miss

Returns:

  • (Array<String>)

    Cached or computed suggestions



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.

Parameters:

  • word (String)

    The word to invalidate

Returns:

  • (self)

    Self for chaining



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.

Parameters:

  • word (String)

    The misspelled word

  • max_results (Integer) (defaults to: 10)

    Max results for this query

Returns:

  • (Boolean)

    True if cached



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.

Parameters:

  • word (String)

    The misspelled word

  • max_results (Integer) (defaults to: 10)

    Max results used for this query

Returns:

  • (Array<String>, nil)

    Cached suggestions or nil



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.

Parameters:

  • word (String)

    The misspelled word

  • suggestions (Array<String>)

    Suggested words

  • max_results (Integer) (defaults to: 10)

    Max results used for this query

Returns:

  • (Array<String>)

    The stored suggestions



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