Class: Jekyll::ClientSearch::IndexCache

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/client_search/index_cache.rb

Overview

Persists content hashes and cached embeddings across Jekyll builds so that unchanged documents are not re-embedded. The cache file lives in the site source directory as .jekyll-client-search-cache.json and should be git-ignored.

Constant Summary collapse

CACHE_FILE =
".jekyll-client-search-cache.json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_source, embedding_identity: nil) ⇒ IndexCache

Returns a new instance of IndexCache.



18
19
20
21
22
# File 'lib/jekyll/client_search/index_cache.rb', line 18

def initialize(site_source, embedding_identity: nil)
  @path = File.join(site_source, CACHE_FILE)
  @embedding_identity = embedding_identity
  @entries = load
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/jekyll/client_search/index_cache.rb', line 16

def path
  @path
end

Class Method Details

.content_hash(document) ⇒ Object



60
61
62
# File 'lib/jekyll/client_search/index_cache.rb', line 60

def self.content_hash(document)
  Digest::SHA256.hexdigest(document.to_json)
end

Instance Method Details

#lookup(id, content_hash) ⇒ Object

Returns the cached entry for id if the content hash and embedding identity match, nil otherwise (or if not cached).



26
27
28
29
30
31
32
33
# File 'lib/jekyll/client_search/index_cache.rb', line 26

def lookup(id, content_hash)
  entry = @entries[id]
  return nil unless entry
  return nil unless entry["content_hash"] == content_hash
  return nil if @embedding_identity && entry["embedding_identity"] != @embedding_identity

  entry
end

#prune(known_ids) ⇒ Object

Removes entries for IDs that are no longer present.



45
46
47
# File 'lib/jekyll/client_search/index_cache.rb', line 45

def prune(known_ids)
  @entries.select! { |id, _| known_ids.include?(id) }
end

#saveObject

Writes the cache to disk if any entries have changed.



50
51
52
53
54
55
56
57
58
# File 'lib/jekyll/client_search/index_cache.rb', line 50

def save
  return unless dirty?

  temporary_path = "#{@path}.tmp.#{Process.pid}.#{Thread.current.object_id}"
  File.write(temporary_path, JSON.pretty_generate(@entries))
  File.rename(temporary_path, @path)
ensure
  FileUtils.rm_f(temporary_path) if temporary_path && File.exist?(temporary_path)
end

#store(id, content_hash, embedding = nil) ⇒ Object

Stores or updates a cache entry for id.



36
37
38
39
40
41
42
# File 'lib/jekyll/client_search/index_cache.rb', line 36

def store(id, content_hash, embedding = nil)
  @entries[id] = {
    "content_hash" => content_hash,
    "embedding" => embedding,
    "embedding_identity" => @embedding_identity
  }.compact
end