Class: ComputeUnit::CacheStore
- Inherits:
-
Object
- Object
- ComputeUnit::CacheStore
- Includes:
- Logger
- Defined in:
- lib/compute_unit/cache_store.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#cache_file ⇒ String
-
the file where the cache is stored, different per user, due to permissions issue.
-
- #cache_files_dir ⇒ Object
- #cache_store ⇒ YAML::Store
-
#initialize(name) ⇒ CacheStore
constructor
A new instance of CacheStore.
- #memory_cache ⇒ Object
- #memory_cache_lookup(key, ttl = 300) ⇒ Object
-
#read_cache(key, default = nil) ⇒ Object
-
returns the stored object given the key, defaults to nil when not found unless specified.
-
-
#read_json_cache_file(key, ttl = 300) ⇒ Object
-
return the parsed json object or nil only if the item ttl has not expired.
-
-
#write_cache(key, value) ⇒ ?
param value [Object] - the object to store in the cache.
-
#write_json_cache_file(key, value) ⇒ ?
-.
- #write_memcache(key, value) ⇒ Object
Methods included from Logger
color, log_file, log_level, logger, #logger
Constructor Details
#initialize(name) ⇒ CacheStore
Returns a new instance of 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
#name ⇒ Object
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_file ⇒ String
Returns - the file where the cache is stored, different per user, due to permissions issue.
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.(File.join(ComputeUnit::CACHE_DIR, name, "#{name}.db")) else File.(File.join('~', '.compute_unit-cache', name, "#{name}.db")) end end |
#cache_files_dir ⇒ Object
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_store ⇒ YAML::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_cache ⇒ Object
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] = cached_item[:ttl] + ttl expired = Time.now.to_i if > 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
Returns - returns the stored object given the key, defaults to nil when not found unless specified.
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.}") default end end |
#read_json_cache_file(key, ttl = 300) ⇒ Object
Returns - return the parsed json object or nil only if the item ttl has not expired.
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) = File.mtime(cache_file).to_i + ttl expired = Time.now.to_i if > 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.}") end end |
#write_json_cache_file(key, value) ⇒ ?
Returns -.
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 |