Class: Todidnt::Cache
- Inherits:
-
Object
- Object
- Todidnt::Cache
- Defined in:
- lib/todidnt/cache.rb
Constant Summary collapse
- CACHE_PATH =
'.todidnt/cache'
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data) ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize(data) ⇒ Cache
Returns a new instance of Cache.
12 13 14 15 |
# File 'lib/todidnt/cache.rb', line 12 def initialize(data) @time = Time.now.to_i @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
10 11 12 |
# File 'lib/todidnt/cache.rb', line 10 def data @data end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
10 11 12 |
# File 'lib/todidnt/cache.rb', line 10 def time @time end |
Class Method Details
.clear! ⇒ Object
42 43 44 45 46 |
# File 'lib/todidnt/cache.rb', line 42 def self.clear! Dir.glob("#{CACHE_PATH}/*").each do |file| File.delete(file) end end |
.exists?(key) ⇒ Boolean
38 39 40 |
# File 'lib/todidnt/cache.rb', line 38 def self.exists?(key) File.exists?("#{CACHE_PATH}/#{key}") end |
.load(key) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/todidnt/cache.rb', line 26 def self.load(key) return nil unless exists?(key) begin raw_cache = File.open("#{CACHE_PATH}/#{key}").read Marshal.load(raw_cache) rescue Exception puts "Cache file was malformed; skipping..." nil end end |
.save(key, data) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/todidnt/cache.rb', line 17 def self.save(key, data) # Create the destinateion folder unless it already exists. FileUtils.mkdir_p(CACHE_PATH) File.open("#{CACHE_PATH}/#{key}", 'w') do |file| file.write(Marshal.dump(Cache.new(data))) end end |