Class: Fontist::Cache::Store

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

Overview

Backend store using Marshal for serialization

Defined Under Namespace

Classes: CacheEntry

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir) ⇒ Store

Returns a new instance of Store.



8
9
10
11
# File 'lib/fontist/cache/store.rb', line 8

def initialize(cache_dir)
  @cache_dir = cache_dir.to_s
  ensure_cache_dir
end

Instance Method Details

#cleanup_temp_filesObject



45
46
47
48
49
50
51
52
53
# File 'lib/fontist/cache/store.rb', line 45

def cleanup_temp_files
  # Clean up orphaned .tmp files from interrupted writes
  # This can happen if the process crashes between File.write and File.rename
  Dir.glob(File.join(@cache_dir, "*.tmp")).each do |tmp|
    File.delete(tmp)
  rescue StandardError
    nil
  end
end

#clearObject



37
38
39
40
41
42
43
# File 'lib/fontist/cache/store.rb', line 37

def clear
  Dir.glob(File.join(@cache_dir, "*.marshal")).each do |f|
    File.delete(f)
  rescue StandardError
    nil
  end
end

#delete(key) ⇒ Object



31
32
33
34
35
# File 'lib/fontist/cache/store.rb', line 31

def delete(key)
  File.delete(cache_path(key))
rescue StandardError
  nil
end

#get(key) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fontist/cache/store.rb', line 13

def get(key)
  entry = read_entry(key)
  return nil unless entry

  if entry.expired?
    delete(key)
    return nil
  end

  entry.value
end

#set(key, value, ttl: nil) ⇒ Object



25
26
27
28
29
# File 'lib/fontist/cache/store.rb', line 25

def set(key, value, ttl: nil)
  ensure_cache_dir
  entry = CacheEntry.new(value, ttl)
  write_entry(key, entry)
end