class Transit::RollingCache

@api private

Constants

CACHE_CODE_DIGITS
CACHE_SIZE
FIRST_ORD
LAST_ORD
MIN_SIZE_CACHEABLE

Public Class Methods

new() click to toggle source
# File lib/transit/rolling_cache.rb, line 28
def initialize
  clear
end

Public Instance Methods

cache_key?(str, _=false) click to toggle source
# File lib/transit/rolling_cache.rb, line 45
def cache_key?(str, _=false)
  str[0] == SUB && str != MAP_AS_ARRAY
end
cacheable?(str, as_map_key=false) click to toggle source
# File lib/transit/rolling_cache.rb, line 49
def cacheable?(str, as_map_key=false)
  str.size >= MIN_SIZE_CACHEABLE && (as_map_key || str.start_with?("~#","~$","~:"))
end
read(key) click to toggle source
# File lib/transit/rolling_cache.rb, line 32
def read(key)
  @key_to_value[key]
end
write(val) click to toggle source
# File lib/transit/rolling_cache.rb, line 36
def write(val)
  @value_to_key[val] || begin
                          clear if @key_to_value.size >= CACHE_SIZE
                          key = next_key(@key_to_value.size)
                          @value_to_key[val] = key
                          @key_to_value[key] = val
                        end
end

Private Instance Methods

clear() click to toggle source
# File lib/transit/rolling_cache.rb, line 55
def clear
  @key_to_value = {}
  @value_to_key = {}
end
next_key(i) click to toggle source
# File lib/transit/rolling_cache.rb, line 60
def next_key(i)
  hi = i / CACHE_CODE_DIGITS;
  lo = i % CACHE_CODE_DIGITS;
  if hi == 0
    "^#{(lo+FIRST_ORD).chr}"
  else
    "^#{(hi+FIRST_ORD).chr}#{(lo+FIRST_ORD).chr}"
  end
end