Class: Prez::Cache
- Inherits:
-
Object
- Object
- Prez::Cache
- Defined in:
- lib/prez/cache.rb
Constant Summary collapse
- DIR =
".prez-cache"
Instance Attribute Summary collapse
-
#toc ⇒ Object
readonly
Returns the value of attribute toc.
Class Method Summary collapse
Instance Method Summary collapse
- #cached_path(key, hash) ⇒ Object
- #dir ⇒ Object
- #get(key, hash) ⇒ Object
- #include?(key, hash) ⇒ Boolean
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #put(key, hash, contents) ⇒ Object
- #save ⇒ Object
- #toc_file ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/prez/cache.rb', line 11 def initialize unless File.directory?(dir) Dir.mkdir dir end if File.exists?(toc_file) @toc = YAML.load File.read(toc_file) else @toc = { prez_version: Prez::Version.to_s } end end |
Instance Attribute Details
#toc ⇒ Object (readonly)
Returns the value of attribute toc.
9 10 11 |
# File 'lib/prez/cache.rb', line 9 def toc @toc end |
Class Method Details
.get(key, pre_cache_contents) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/prez/cache.rb', line 73 def get(key, pre_cache_contents) hash = md5 pre_cache_contents if instance.include?(key, hash) instance.get key, hash else yield.tap do |results| instance.put key, hash, results end end end |
.instance ⇒ Object
89 90 91 |
# File 'lib/prez/cache.rb', line 89 def instance @instance ||= Prez::Cache.new end |
.md5(contents) ⇒ Object
85 86 87 |
# File 'lib/prez/cache.rb', line 85 def md5(contents) Digest::MD5.hexdigest contents end |
Instance Method Details
#cached_path(key, hash) ⇒ Object
35 36 37 |
# File 'lib/prez/cache.rb', line 35 def cached_path(key, hash) File.join dir, Prez::Cache.md5(key), hash end |
#get(key, hash) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/prez/cache.rb', line 39 def get(key, hash) File.read cached_path(key, hash) ensure toc[key][:last_touched][hash] = Time.now save end |
#include?(key, hash) ⇒ Boolean
31 32 33 |
# File 'lib/prez/cache.rb', line 31 def include?(key, hash) toc.include?(key) && File.file?(cached_path(key, hash)) end |
#put(key, hash, contents) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/prez/cache.rb', line 46 def put(key, hash, contents) unless include?(key, hash) toc[key] = { last_touched: {}, saved: {} } end path = cached_path key, hash unless File.directory?(File.dirname(path)) FileUtils.makedirs File.dirname(path) end File.write path, contents toc[key][:key_hash] = Prez::Cache.md5 key toc[key][:last_touched][hash] = Time.now toc[key][:saved][hash] = Time.now ensure save end |
#save ⇒ Object
68 69 70 |
# File 'lib/prez/cache.rb', line 68 def save File.write toc_file, YAML.dump(toc) end |
#toc_file ⇒ Object
27 28 29 |
# File 'lib/prez/cache.rb', line 27 def toc_file File.join dir, "toc.yml" end |