Class: Smplkit::Flags::ResolutionCache Private

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/flags/client.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Thread-safe LRU resolution cache with hit/miss stats.

Constant Summary collapse

DEFAULT_MAX_SIZE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ ResolutionCache

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ResolutionCache.



72
73
74
75
76
77
78
# File 'lib/smplkit/flags/client.rb', line 72

def initialize(max_size: DEFAULT_MAX_SIZE)
  @max_size = max_size
  @cache = {}
  @lock = Mutex.new
  @cache_hits = 0
  @cache_misses = 0
end

Instance Attribute Details

#cache_hitsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
# File 'lib/smplkit/flags/client.rb', line 70

def cache_hits
  @cache_hits
end

#cache_missesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
# File 'lib/smplkit/flags/client.rb', line 70

def cache_misses
  @cache_misses
end

Instance Method Details

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
# File 'lib/smplkit/flags/client.rb', line 103

def clear
  @lock.synchronize { @cache.clear }
end

#get(cache_key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return [hit, value]. Moves the key to end on hit.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/smplkit/flags/client.rb', line 81

def get(cache_key)
  @lock.synchronize do
    if @cache.key?(cache_key)
      value = @cache.delete(cache_key)
      @cache[cache_key] = value
      @cache_hits += 1
      [true, value]
    else
      @cache_misses += 1
      [false, nil]
    end
  end
end

#put(cache_key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
98
99
100
101
# File 'lib/smplkit/flags/client.rb', line 95

def put(cache_key, value)
  @lock.synchronize do
    @cache.delete(cache_key) if @cache.key?(cache_key)
    @cache[cache_key] = value
    @cache.shift while @cache.size > @max_size
  end
end