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.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/prez/cache.rb', line 12 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.
10 11 12 |
# File 'lib/prez/cache.rb', line 10 def toc @toc end |
Class Method Details
.get(key, pre_cache_contents) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/prez/cache.rb', line 74 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
90 91 92 |
# File 'lib/prez/cache.rb', line 90 def instance @instance ||= Prez::Cache.new end |
.md5(contents) ⇒ Object
86 87 88 |
# File 'lib/prez/cache.rb', line 86 def md5(contents) Digest::MD5.hexdigest contents end |
Instance Method Details
#cached_path(key, hash) ⇒ Object
36 37 38 |
# File 'lib/prez/cache.rb', line 36 def cached_path(key, hash) File.join dir, Prez::Cache.md5(key), hash end |
#dir ⇒ Object
24 25 26 |
# File 'lib/prez/cache.rb', line 24 def dir Prez::Cache::DIR end |
#get(key, hash) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/prez/cache.rb', line 40 def get(key, hash) File.read cached_path(key, hash) ensure toc[key][:last_touched][hash] = Time.now save end |
#include?(key, hash) ⇒ Boolean
32 33 34 |
# File 'lib/prez/cache.rb', line 32 def include?(key, hash) toc.include?(key) && File.file?(cached_path(key, hash)) end |
#put(key, hash, contents) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/prez/cache.rb', line 47 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
69 70 71 |
# File 'lib/prez/cache.rb', line 69 def save File.write toc_file, YAML.dump(toc) end |
#toc_file ⇒ Object
28 29 30 |
# File 'lib/prez/cache.rb', line 28 def toc_file File.join dir, "toc.yml" end |