class Cache

Attributes

expiry_time[RW]
max_size[RW]
post_get[RW]
refresh[RW]
store[RW]

Public Class Methods

new() click to toggle source

Creates a basic Cache with the UTC timezone

# File lib/libcache/cache.rb, line 9
def initialize
  ENV['TZ'] = 'UTC'
  @scheduler = Rufus::Scheduler.new
  @time_tracker = Hash.new
end

Public Instance Methods

check_refresh(key) click to toggle source

Refreshes an object if it has been invalidated @param [String] key The key value used to identify an object in the cache @return [Object] The refreshed object that is recalled from the refresh method

# File lib/libcache/cache.rb, line 70
def check_refresh(key)
  if @cache[key] == nil && !has_refresh?
    val = refresh.call(key)
    put(key, val)
    return val
  end
end
create_store() click to toggle source

Initializes the cache store

# File lib/libcache/cache.rb, line 16
def create_store
  @cache = Hash.new
end
exists?(key) click to toggle source

Checks if a key-value pair exists in the cache @param [String] key The key value used to identify an object in the cache @return [Boolean] The existence of the key in the cache

# File lib/libcache/cache.rb, line 63
def exists?(key)
  return @cache.key?(key)
end
get(key) click to toggle source

Gets the object that corresponds with the key @param [String] key The key value used to identify an object in the cache @return [Object] The object that corresponds with the key

# File lib/libcache/cache.rb, line 51
def get(key)
  check_refresh(key)
  if(@cache[key]) == nil
    return nil
  end
  perform_post_get(key)
  return @cache[key]
end
get_size() click to toggle source
# File lib/libcache/cache.rb, line 91
def get_size
  return @cache.length
end
has_refresh?() click to toggle source

@return [Boolean] Checks if the cache has a refresh method

# File lib/libcache/cache.rb, line 87
def has_refresh?
  return refresh == nil
end
invalidate(key) click to toggle source

Deletes a key-value pair from the cache @param [String] key The key value used to identify an object in the cache

# File lib/libcache/cache.rb, line 97
def invalidate(key)
  @cache.delete key
end
invalidate_all() click to toggle source

Clears all items in the cache

# File lib/libcache/cache.rb, line 102
def invalidate_all
  @cache.clear
end
perform_post_get(key) click to toggle source

Performs a function after a key has been summoned from the cache @param [String] key The key value used to identify an object in the cache

# File lib/libcache/cache.rb, line 80
def perform_post_get(key)
  unless post_get.nil?
    post_get.call(key)
  end
end
put(key, value) click to toggle source

Places an object inside the cache and handles max size eviction @param [String] key The key value used to identify an object in the cache @param [Object] value The object to be placed in the cache

# File lib/libcache/cache.rb, line 23
def put(key, value)
  @cache[key] = value
  if expiry_time != nil
    @scheduler.in expiry_time, :blocking => true do
      invalidate key
    end
  end
  check_expiration(key)
end

Private Instance Methods

check_expiration(key) click to toggle source
# File lib/libcache/cache.rb, line 33
        def check_expiration(key)
  # removed oldest entry if max_size is approached
  @time_tracker[key] = Time.now
  if max_size != nil
    if get_size >= max_size
      n = get_size - max_size
      keys = @time_tracker.sort_by{|k,v| Time.now - v }.reverse.first n
      keys.each do |keyz, valuez|
        invalidate(keyz)
        @time_tracker.delete(keyz)
      end
    end
  end
end