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.
-
#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 |
# File 'lib/cache_tree.rb', line 71 def initialize(target) @name = target.class.name.gsub(/([A-Z])/) { "_#{$1}" }.gsub(/^./, '').downcase @value = target.id @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.
91 92 93 |
# File 'lib/cache_tree.rb', line 91 def btree_key directory + SEPARATOR + (map(:up) { |node| node.path } * '') + 'btree.key' end |
#cache_file ⇒ Object
Resolves final name for cache file.
112 113 114 |
# File 'lib/cache_tree.rb', line 112 def cache_file directory + SEPARATOR + (map(:up) { |node| node.path } * '') + checksum + '.cache' end |
#checksum ⇒ Object
Sums up all stamps and generates a checksum.
107 108 109 |
# File 'lib/cache_tree.rb', line 107 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.
136 137 138 |
# File 'lib/cache_tree.rb', line 136 def clean diagnose[:dead].each { |file| FileUtils.rm file } end |
#debug ⇒ Object
Prints out active cache in green, and expired files in red.
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/cache_tree.rb', line 160 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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cache_tree.rb', line 144 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
77 78 79 |
# File 'lib/cache_tree.rb', line 77 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.
125 126 127 128 |
# File 'lib/cache_tree.rb', line 125 def expire @stamp = generate_stamp save end |
#generate_stamp ⇒ Object
81 82 83 |
# File 'lib/cache_tree.rb', line 81 def generate_stamp (Time.now.to_f * 1.0).to_s + '-' + Kernel.rand(1000000).to_s end |
#load ⇒ Object
Updates current node stamp from btree_key
96 97 98 |
# File 'lib/cache_tree.rb', line 96 def load @stamp = eval(File.read(btree_key)) end |
#load! ⇒ Object
Updates current node stamp from btree_key ensuring the btree file is present reflecting node-data on memory.
102 103 104 |
# File 'lib/cache_tree.rb', line 102 def load! File.exists?(btree_key) ? load : save end |
#path ⇒ Object
Path of the cache node
86 87 88 |
# File 'lib/cache_tree.rb', line 86 def path "#{name}#{SEPARATOR}#{value}#{SEPARATOR}" end |
#save ⇒ Object
Writes node stamp from to btree_key file
117 118 119 120 |
# File 'lib/cache_tree.rb', line 117 def save FileUtils.mkdir_p File.dirname(btree_key) File.open(btree_key, 'w+') { |file| file.write stamp.inspect } end |