Class: Shugoi::HtmlStore

Inherits:
Object
  • Object
show all
Defined in:
lib/shugoi/html_store.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

TOKEN_TTL_MS =
120_000
MAX_ENTRIES =
5000
MAX_TOTAL_BYTES =
64 * 1024 * 1024
MAX_TOKEN_READS =
1

Instance Method Summary collapse

Constructor Details

#initialize(disk_path: nil) ⇒ HtmlStore

Returns a new instance of HtmlStore.



10
11
12
13
14
15
16
# File 'lib/shugoi/html_store.rb', line 10

def initialize(disk_path: nil)
  @entries = {}
  @site_cache = {}
  @total_bytes = 0
  @disk_path = disk_path
  @mutex = Mutex.new
end

Instance Method Details

#drop(token) ⇒ Object



48
49
50
# File 'lib/shugoi/html_store.rb', line 48

def drop(token)
  @mutex.synchronize { drop_unlocked(token) }
end

#read(token) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/shugoi/html_store.rb', line 33

def read(token)
  @mutex.synchronize do
    entry = @entries[token]
    return nil if entry.nil?
    return drop_and_nil(token) if Utils.now_ms > entry.expires_at

    entry.reads += 1
    entry.html
  end
end

#site_html(site_key) ⇒ Object



44
45
46
# File 'lib/shugoi/html_store.rb', line 44

def site_html(site_key)
  @site_cache[site_key]
end

#store(token, html, site_key) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shugoi/html_store.rb', line 18

def store(token, html, site_key)
  @mutex.synchronize do
    evict_expired
    size = html.bytesize
    while (@entries.size >= MAX_ENTRIES || @total_bytes + size > MAX_TOTAL_BYTES) && !@entries.empty?
      drop_unlocked(@entries.keys.first)
    end
    drop_unlocked(token)
    @entries[token] = Entry.new(html, Utils.now_ms + TOKEN_TTL_MS, 0)
    @site_cache[site_key] = html
    @total_bytes += size
    write_disk(token, html)
  end
end