Class: Chelsea::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/chelsea/db.rb

Overview

OSS Index data cache

Instance Method Summary collapse

Constructor Details

#initializeDB

Returns a new instance of DB.



24
25
26
# File 'lib/chelsea/db.rb', line 24

def initialize
  @store = PStore.new(_get_db_store_location)
end

Instance Method Details

#_get_db_store_locationObject



51
52
53
54
55
# 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_cacheObject

This method will delete all values in the pstore db



43
44
45
46
47
48
49
# 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) ⇒ Object

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



60
61
62
63
64
65
# 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) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
40
# 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