Class: HTTPDisk::Cache
- Inherits:
-
Object
- Object
- HTTPDisk::Cache
- Defined in:
- lib/httpdisk/cache.rb
Overview
Disk cache for cache_keys => response. Files are compressed.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#diskpath(cache_key) ⇒ Object
Relative path for this cache_key based on the cache key.
-
#initialize(options) ⇒ Cache
constructor
A new instance of Cache.
-
#read(cache_key) ⇒ Object
Get cached response.
-
#status(cache_key) ⇒ Object
Cache status for a cache_key, %i[error force hit miss stale].
-
#write(cache_key, payload) ⇒ Object
Write response to the disk cache.
Constructor Details
#initialize(options) ⇒ Cache
Returns a new instance of Cache.
8 9 10 |
# File 'lib/httpdisk/cache.rb', line 8 def initialize() @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/httpdisk/cache.rb', line 6 def @options end |
Instance Method Details
#diskpath(cache_key) ⇒ Object
Relative path for this cache_key based on the cache key
42 43 44 |
# File 'lib/httpdisk/cache.rb', line 42 def diskpath(cache_key) File.join(dir, cache_key.diskpath) end |
#read(cache_key) ⇒ Object
Get cached response. If there is a cached error it will be raised.
21 22 23 24 |
# File 'lib/httpdisk/cache.rb', line 21 def read(cache_key) payload_or_status = read0(cache_key) payload_or_status.is_a?(Symbol) ? nil : payload_or_status end |
#status(cache_key) ⇒ Object
Cache status for a cache_key, %i[error force hit miss stale]
27 28 29 30 31 32 |
# File 'lib/httpdisk/cache.rb', line 27 def status(cache_key) payload_or_status = read0(cache_key, peek: true) return payload_or_status if payload_or_status.is_a?(Symbol) payload_or_status.error? ? :error : :hit end |
#write(cache_key, payload) ⇒ Object
Write response to the disk cache
35 36 37 38 39 |
# File 'lib/httpdisk/cache.rb', line 35 def write(cache_key, payload) path = diskpath(cache_key) FileUtils.mkdir_p(File.dirname(path)) Zlib::GzipWriter.open(path) { payload.write(_1) } end |