Class: CMSScanner::Cache::FileStore
- Inherits:
-
Object
- Object
- CMSScanner::Cache::FileStore
- Defined in:
- lib/cms_scanner/cache/file_store.rb
Overview
Cache Implementation using files
Direct Known Subclasses
Instance Attribute Summary collapse
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
-
#storage_path ⇒ Object
readonly
Returns the value of attribute storage_path.
Instance Method Summary collapse
-
#clean ⇒ Object
TODO: rename this to clear ?.
-
#entry_expiration_path(key) ⇒ String
The expiration file path associated to the key.
-
#entry_path(key) ⇒ String
The file path associated to the key.
-
#initialize(storage_path, serializer = Marshal) ⇒ FileStore
constructor
The serializer must have the 2 methods #load and #dump (Marshal and YAML have them) YAML is Human Readable, contrary to Marshal which store in a binary format Marshal does not need any “require”.
- #read_entry(key) ⇒ Mixed
- #write_entry(key, data_to_store, cache_ttl) ⇒ Object
Constructor Details
#initialize(storage_path, serializer = Marshal) ⇒ FileStore
The serializer must have the 2 methods #load and #dump
(Marshal and YAML have them)
YAML is Human Readable, contrary to Marshal which store in a binary format Marshal does not need any “require”
16 17 18 19 20 21 |
# File 'lib/cms_scanner/cache/file_store.rb', line 16 def initialize(storage_path, serializer = Marshal) @storage_path = File.(storage_path) @serializer = serializer FileUtils.mkdir_p(@storage_path) unless Dir.exist?(@storage_path) end |
Instance Attribute Details
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
7 8 9 |
# File 'lib/cms_scanner/cache/file_store.rb', line 7 def serializer @serializer end |
#storage_path ⇒ Object (readonly)
Returns the value of attribute storage_path.
7 8 9 |
# File 'lib/cms_scanner/cache/file_store.rb', line 7 def storage_path @storage_path end |
Instance Method Details
#clean ⇒ Object
TODO: rename this to clear ?
24 25 26 27 28 |
# File 'lib/cms_scanner/cache/file_store.rb', line 24 def clean Dir[File.join(storage_path, '*')].each do |f| File.delete(f) unless File.symlink?(f) end end |
#entry_expiration_path(key) ⇒ String
Returns The expiration file path associated to the key.
61 62 63 |
# File 'lib/cms_scanner/cache/file_store.rb', line 61 def entry_expiration_path(key) "#{entry_path(key)}.expiration" end |
#entry_path(key) ⇒ String
Returns The file path associated to the key.
54 55 56 |
# File 'lib/cms_scanner/cache/file_store.rb', line 54 def entry_path(key) File.join(storage_path, key) end |
#read_entry(key) ⇒ Mixed
33 34 35 36 37 38 39 |
# File 'lib/cms_scanner/cache/file_store.rb', line 33 def read_entry(key) return if expired_entry?(key) serializer.load(File.read(entry_path(key))) rescue StandardError nil end |
#write_entry(key, data_to_store, cache_ttl) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/cms_scanner/cache/file_store.rb', line 44 def write_entry(key, data_to_store, cache_ttl) return unless cache_ttl.to_i.positive? File.write(entry_path(key), serializer.dump(data_to_store)) File.write(entry_expiration_path(key), Time.now.to_i + cache_ttl) end |