class Chelsea::DB

OSS Index data cache

Public Class Methods

new() click to toggle source
# File lib/chelsea/db.rb, line 24
def initialize
  @store = PStore.new(_get_db_store_location)
end

Public Instance Methods

_get_db_store_location() click to toggle source
# File lib/chelsea/db.rb, line 51
def _get_db_store_location
  initial_path = File.join(Dir.home.to_s, '.ossindex')
  Dir.mkdir(initial_path) unless File.exist? initial_path
  File.join(initial_path, 'chelsea.pstore')
end
clear_cache() click to toggle source

This method will delete all values in the pstore db

# File lib/chelsea/db.rb, line 43
def clear_cache
  @store.transaction do
    @store.roots.each do |root|
      @store.delete(root)
    end
  end
end
get_cached_value_from_db(val) click to toggle source

Checks pstore to see if a coordinate exists, and if it does also checks to see if it's ttl has expired. Returns nil unless a record is valid in the cache (ttl has not expired) and found

# File lib/chelsea/db.rb, line 60
def get_cached_value_from_db(val)
  record = @store.transaction { @store[val] }
  return if record.nil?

  (Time.now - record['ttl']) / 3600 > 12 ? nil : record
end
save_values_to_db(values) click to toggle source

This method will take an array of values, and save them to a pstore database and as well set a TTL of Time.now to be checked later

# File lib/chelsea/db.rb, line 30
def save_values_to_db(values)
  values.each do |val|
    next unless get_cached_value_from_db(val['coordinates']).nil?

    new_val = val.dup
    new_val['ttl'] = Time.now
    @store.transaction do
      @store[new_val['coordinates']] = new_val
    end
  end
end