Class: ComputeUnit::CacheStore

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/compute_unit/cache_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

color, log_file, log_level, logger, #logger

Constructor Details

#initialize(name) ⇒ CacheStore



12
13
14
15
16
# File 'lib/compute_unit/cache_store.rb', line 12

def initialize(name)
  @name = name
  FileUtils.mkdir_p(File.dirname(cache_file))
  FileUtils.mkdir_p(cache_files_dir)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/compute_unit/cache_store.rb', line 9

def name
  @name
end

Instance Method Details

#cache_fileString



28
29
30
31
32
33
34
# File 'lib/compute_unit/cache_store.rb', line 28

def cache_file
  if Etc.getpwuid.name == 'root'
    File.expand_path(File.join(ComputeUnit::CACHE_DIR, name, "#{name}.db"))
  else
    File.expand_path(File.join('~', '.compute_unit-cache', name, "#{name}.db"))
  end
end

#cache_files_dirObject



18
19
20
# File 'lib/compute_unit/cache_store.rb', line 18

def cache_files_dir
  File.join(File.dirname(cache_file), 'files')
end

#cache_storeYAML::Store



23
24
25
# File 'lib/compute_unit/cache_store.rb', line 23

def cache_store
  @cache_store ||= ::YAML::Store.new(cache_file)
end

#memory_cacheObject



67
68
69
# File 'lib/compute_unit/cache_store.rb', line 67

def memory_cache
  @memory_cache ||= {}
end

#memory_cache_lookup(key, ttl = 300) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/compute_unit/cache_store.rb', line 80

def memory_cache_lookup(key, ttl = 300)
  return unless ttl.positive?

  logger.debug("Looking up memory cache for #{key}")
  hexkey = Digest::SHA1.hexdigest(key)
  cached_item = memory_cache[hexkey]
  return nil unless cached_item && cached_item[:content]

  timestamp = cached_item[:ttl] + ttl
  expired = Time.now.to_i
  if timestamp > expired
    logger.debug("Memory cache hit #{hexkey}")
    begin
      cached_item[:content]
    rescue JSON::ParserError
      nil
    end
  else
    logger.debug("Cleaning cache for #{hexkey}")
    memory_cache.delete(hexkey)
    nil
  end
end

#read_cache(key, default = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/compute_unit/cache_store.rb', line 39

def read_cache(key, default = nil)
  return default unless key

  begin
    cache_store.transaction do
      cache_store.fetch(key, default)
    end
  rescue StandardError => e
    logger.debug("Error reading from cache with key #{key}: #{e.message}")
    default
  end
end

#read_json_cache_file(key, ttl = 300) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/compute_unit/cache_store.rb', line 107

def read_json_cache_file(key, ttl = 300)
  logger.debug("Looking up cache for #{key}")
  hexkey = Digest::SHA1.hexdigest(key)
  cache_file = File.join(cache_files_dir, "#{hexkey}.json")
  return nil unless cache_file
  return nil unless File.exist?(cache_file)

  timestamp = File.mtime(cache_file).to_i + ttl
  expired = Time.now.to_i
  if timestamp > expired
    logger.debug("Cache hit #{hexkey}")
    begin
      JSON.parse(File.read(cache_file))
    rescue JSON::ParserError
      nil
    end
  else
    logger.debug("Cleaning cache for #{hexkey}")
    FileUtils.rm(cache_file)
    nil
  end
end

#write_cache(key, value) ⇒ ?

param value [Object] - the object to store in the cache



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/compute_unit/cache_store.rb', line 55

def write_cache(key, value)
  return if ENV['XB_DISABLE_CACHE'] =~ /yes|true/i

  begin
    cache_store.transaction do
      cache_store[key] = value
    end
  rescue StandardError => e
    logger.debug("Error writing to cache with key #{key}: #{e.message}")
  end
end

#write_json_cache_file(key, value) ⇒ ?



133
134
135
136
137
138
139
140
141
# File 'lib/compute_unit/cache_store.rb', line 133

def write_json_cache_file(key, value)
  return if ENV['XB_DISABLE_CACHE'] =~ /yes|true/i

  logger.debug("Caching content with key : #{key}")
  cache_file = File.join(cache_files_dir, "#{key}.json")
  File.open(cache_file, 'w') do |f|
    f.write(value)
  end
end

#write_memcache(key, value) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/compute_unit/cache_store.rb', line 71

def write_memcache(key, value)
  logger.debug("Caching content with key : #{key}")
  memory_cache[key] = {
    content: value,
    ttl: Time.now.to_i
  }
  value
end