Class: Ficon::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/ficon/cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Cache

Returns a new instance of Cache.



5
6
7
8
# File 'lib/ficon/cache.rb', line 5

def initialize(url)
  @url = url.to_s
  Cache.setup_cache(db) if db.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='urls'").length == 0
end

Class Method Details

.clear_cacheObject



83
84
85
# File 'lib/ficon/cache.rb', line 83

def self.clear_cache
  File.delete(db_file) if File.exist?(db_file)
end

.db_fileObject



70
71
72
73
74
75
76
# File 'lib/ficon/cache.rb', line 70

def self.db_file
  if ENV["FICON_DB"].nil?
    File.expand_path("~/.ficon.db")
  else
    ENV["FICON_DB"]
  end
end

.setup_cache(db) ⇒ Object



78
79
80
81
# File 'lib/ficon/cache.rb', line 78

def self.setup_cache(db)
  db.execute("CREATE TABLE urls(url, etag, not_before, data, status, retry_count, last_attempt)")
  db.execute("CREATE UNIQUE INDEX `url` ON `urls` (`url`)")
end

Instance Method Details

#dataObject



16
17
18
# File 'lib/ficon/cache.rb', line 16

def data
  db.execute("select data from urls where url=? limit 1", @url).first&.first
end

#data=(_value) ⇒ Object



20
21
22
23
# File 'lib/ficon/cache.rb', line 20

def data=(_value)
  db.execute("INSERT OR IGNORE INTO urls (url, data) VALUES (?, ?)", [@url, _value])
  db.execute("UPDATE urls SET data=? WHERE url=?", [_value, @url])
end

#dbObject



10
11
12
13
14
# File 'lib/ficon/cache.rb', line 10

def db
  _db = SQLite3::Database.new Cache.db_file
  _db.busy_timeout = 1000
  _db
end

#etagObject



25
26
27
# File 'lib/ficon/cache.rb', line 25

def etag
  db.execute("select etag from urls where url=? limit 1", @url).first&.first
end

#etag=(_value) ⇒ Object



29
30
31
32
# File 'lib/ficon/cache.rb', line 29

def etag=(_value)
  db.execute("INSERT OR IGNORE INTO urls (url, etag) VALUES (?, ?)", [@url, _value])
  db.execute("UPDATE urls SET etag=? WHERE url=?", [_value, @url])
end

#last_attemptObject



61
62
63
# File 'lib/ficon/cache.rb', line 61

def last_attempt
  db.execute("select last_attempt from urls where url=? limit 1", @url).first&.first
end

#last_attempt=(_value) ⇒ Object



65
66
67
68
# File 'lib/ficon/cache.rb', line 65

def last_attempt=(_value)
  db.execute("INSERT OR IGNORE INTO urls (url, last_attempt) VALUES (?, ?)", [@url, _value])
  db.execute("UPDATE urls SET last_attempt=? WHERE url=?", [_value, @url])
end

#not_beforeObject



34
35
36
# File 'lib/ficon/cache.rb', line 34

def not_before
  db.execute("select not_before from urls where url=? limit 1", @url).first&.first
end

#not_before=(_value) ⇒ Object



38
39
40
41
# File 'lib/ficon/cache.rb', line 38

def not_before=(_value)
  db.execute("INSERT OR IGNORE INTO urls (url, not_before) VALUES (?, ?)", [@url, _value])
  db.execute("UPDATE urls SET not_before=? WHERE url=?", [_value, @url])
end

#retry_countObject



52
53
54
# File 'lib/ficon/cache.rb', line 52

def retry_count
  db.execute("select retry_count from urls where url=? limit 1", @url).first&.first || 0
end

#retry_count=(_value) ⇒ Object



56
57
58
59
# File 'lib/ficon/cache.rb', line 56

def retry_count=(_value)
  db.execute("INSERT OR IGNORE INTO urls (url, retry_count) VALUES (?, ?)", [@url, _value])
  db.execute("UPDATE urls SET retry_count=? WHERE url=?", [_value, @url])
end

#statusObject



43
44
45
# File 'lib/ficon/cache.rb', line 43

def status
  db.execute("select status from urls where url=? limit 1", @url).first&.first
end

#status=(_value) ⇒ Object



47
48
49
50
# File 'lib/ficon/cache.rb', line 47

def status=(_value)
  db.execute("INSERT OR IGNORE INTO urls (url, status) VALUES (?, ?)", [@url, _value])
  db.execute("UPDATE urls SET status=? WHERE url=?", [_value, @url])
end