Class: Shugoi::HtmlStore

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

Overview

Stockage mémoire (et disque optionnel) du HTML rendu, lié au token. Parité avec _memoryStore/_siteCache (render.ts).

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.



14
15
16
17
18
19
20
# File 'lib/shugoi/html_store.rb', line 14

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



52
53
54
55
56
57
# File 'lib/shugoi/html_store.rb', line 52

def drop(token)
  @mutex.synchronize do
    entry = @entries.delete(token)
    @total_bytes -= entry.html.bytesize if entry
  end
end

#read(token) ⇒ String?

Returns html si présent et lisible.

Returns:

  • (String, nil)

    html si présent et lisible



37
38
39
40
41
42
43
44
45
46
# File 'lib/shugoi/html_store.rb', line 37

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



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

def site_html(site_key)
  @site_cache[site_key]
end

#store(token, html, site_key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shugoi/html_store.rb', line 22

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(@entries.keys.first)
    end
    @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