Class: CacheTree::Node
Constant Summary collapse
- SEPARATOR =
'/'
Class Attribute Summary collapse
-
.directory ⇒ Object
Returns the value of attribute directory.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#stamp ⇒ Object
Returns the value of attribute stamp.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#btree_key ⇒ Object
Resolves final name for btree_key file.
-
#cache_file ⇒ Object
Resolves final name for cache file.
-
#checksum ⇒ Object
Sums up all stamps and generates a checksum.
-
#clean ⇒ Array
Cleans up expired cache files for a specific node.
-
#debug ⇒ Object
Prints out active cache in green, and expired files in red.
-
#diagnose ⇒ Hash
Runs an analysis on a given node and returns its status.
- #directory ⇒ Object
-
#expire ⇒ Object
Quickly expire a whole cache-tree or a single node.
- #generate_stamp ⇒ Object
-
#initialize(target) ⇒ Node
constructor
A new instance of Node.
- #initialize_from_hash(hash) ⇒ Object
- #initialize_from_object(target) ⇒ Object
-
#load ⇒ Object
Updates current node stamp from btree_key.
-
#load! ⇒ Object
Updates current node stamp from btree_key ensuring the btree file is present reflecting node-data on memory.
-
#path ⇒ Object
Path of the cache node.
-
#save ⇒ Object
Writes node stamp from to btree_key file.
Constructor Details
#initialize(target) ⇒ Node
Returns a new instance of Node.
71 72 73 74 75 76 77 78 |
# File 'lib/cache_tree.rb', line 71 def initialize(target) if target.is_a?(Hash) initialize_from_hash(target) else initialize_from_object(target) end @stamp = generate_stamp end |
Class Attribute Details
.directory ⇒ Object
Returns the value of attribute directory.
62 63 64 |
# File 'lib/cache_tree.rb', line 62 def directory @directory end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
67 68 69 |
# File 'lib/cache_tree.rb', line 67 def name @name end |
#stamp ⇒ Object
Returns the value of attribute stamp.
67 68 69 |
# File 'lib/cache_tree.rb', line 67 def stamp @stamp end |
#value ⇒ Object
Returns the value of attribute value.
67 68 69 |
# File 'lib/cache_tree.rb', line 67 def value @value end |
Instance Method Details
#btree_key ⇒ Object
Resolves final name for btree_key file.
104 105 106 |
# File 'lib/cache_tree.rb', line 104 def btree_key directory + SEPARATOR + (map(:up) { |node| node.path } * '') + 'btree.key' end |
#cache_file ⇒ Object
Resolves final name for cache file.
125 126 127 |
# File 'lib/cache_tree.rb', line 125 def cache_file directory + SEPARATOR + (map(:up) { |node| node.path } * '') + checksum + '.cache' end |
#checksum ⇒ Object
Sums up all stamps and generates a checksum.
120 121 122 |
# File 'lib/cache_tree.rb', line 120 def checksum MD5.hexdigest(map(:up) { |node| node.stamp } * '') end |
#clean ⇒ Array
Cleans up expired cache files for a specific node.
You need to be specific the engine will not walk down the tree for you to prevent iterating through large trees.
149 150 151 |
# File 'lib/cache_tree.rb', line 149 def clean diagnose[:dead].each { |file| FileUtils.rm file } end |
#debug ⇒ Object
Prints out active cache in green, and expired files in red.
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/cache_tree.rb', line 173 def debug diagnostic = diagnose if File.exists?(diagnostic[:alive]) puts diagnostic[:alive].to_ansi.green else puts diagnostic[:alive].to_ansi.yellow end diagnostic[:dead].each { |file| puts file.to_ansi.red } nil end |
#diagnose ⇒ Hash
Runs an analysis on a given node and returns its status.
node’s cache
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/cache_tree.rb', line 157 def diagnose map(:up) { |node| node.load! } file_alive = cache_file base = File.basename(file_alive) dir = File.dirname(file_alive) diagnostic = {} diagnostic[:alive] = file_alive diagnostic[:dead] = [] Dir["#{dir}/*.cache"].each do |cached_file| next if File.basename(cached_file) == base diagnostic[:dead] << cached_file end diagnostic end |
#directory ⇒ Object
90 91 92 |
# File 'lib/cache_tree.rb', line 90 def directory self.class.directory end |
#expire ⇒ Object
Quickly expire a whole cache-tree or a single node. Expiring a node in the middle will automatically expire all children nodes. no need to expire each one individually.
138 139 140 141 |
# File 'lib/cache_tree.rb', line 138 def expire @stamp = generate_stamp save end |
#generate_stamp ⇒ Object
94 95 96 |
# File 'lib/cache_tree.rb', line 94 def generate_stamp (Time.now.to_f * 1.0).to_s + '-' + Kernel.rand(1000000).to_s end |
#initialize_from_hash(hash) ⇒ Object
80 81 82 83 |
# File 'lib/cache_tree.rb', line 80 def initialize_from_hash(hash) @name = hash[:name] @value = hash[:value] end |
#initialize_from_object(target) ⇒ Object
85 86 87 88 |
# File 'lib/cache_tree.rb', line 85 def initialize_from_object(target) @name = target.class.name.gsub(/([A-Z])/) { "_#{$1}" }.gsub(/^./, '').downcase @value = target.id end |
#load ⇒ Object
Updates current node stamp from btree_key
109 110 111 |
# File 'lib/cache_tree.rb', line 109 def load @stamp = eval(File.read(btree_key).to_s.inspect) end |
#load! ⇒ Object
Updates current node stamp from btree_key ensuring the btree file is present reflecting node-data on memory.
115 116 117 |
# File 'lib/cache_tree.rb', line 115 def load! File.exists?(btree_key) ? load : save end |
#path ⇒ Object
Path of the cache node
99 100 101 |
# File 'lib/cache_tree.rb', line 99 def path "#{name}#{SEPARATOR}#{value}#{SEPARATOR}" end |
#save ⇒ Object
Writes node stamp from to btree_key file
130 131 132 133 |
# File 'lib/cache_tree.rb', line 130 def save FileUtils.mkdir_p File.dirname(btree_key) File.open(btree_key, 'w+') { |file| file.write stamp.inspect } end |