Class: CacheTree::Node

Inherits:
Object
  • Object
show all
Includes:
Tree::Node
Defined in:
lib/cache_tree.rb

Constant Summary collapse

SEPARATOR =
'/'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Node

Returns a new instance of Node.



76
77
78
79
80
81
82
83
# File 'lib/cache_tree.rb', line 76

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

.directoryObject

Returns the value of attribute directory.



67
68
69
# File 'lib/cache_tree.rb', line 67

def directory
  @directory
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



72
73
74
# File 'lib/cache_tree.rb', line 72

def name
  @name
end

#stampObject

Returns the value of attribute stamp.



72
73
74
# File 'lib/cache_tree.rb', line 72

def stamp
  @stamp
end

#valueObject

Returns the value of attribute value.



72
73
74
# File 'lib/cache_tree.rb', line 72

def value
  @value
end

Instance Method Details

#_md5_Object



124
125
126
127
128
129
130
# File 'lib/cache_tree.rb', line 124

def _md5_
  if RUBY_VERSION == "1.8.7"
    MD5
  else
    Digest::MD5
  end
end

#btree_keyObject

Resolves final name for btree_key file.



109
110
111
# File 'lib/cache_tree.rb', line 109

def btree_key
  directory + SEPARATOR + (map(:up) { |node| node.path } * '') + 'btree.key'
end

#cache_fileObject

Resolves final name for cache file.



138
139
140
# File 'lib/cache_tree.rb', line 138

def cache_file
  directory + SEPARATOR + (map(:up) { |node| node.path } * '') + checksum + '.cache'
end

#checksumObject

Sums up all stamps and generates a checksum.



133
134
135
# File 'lib/cache_tree.rb', line 133

def checksum
  _md5_.hexdigest(map(:up) { |node| node.stamp } * '')
end

#cleanArray

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.

Returns:

  • (Array)

    of items that were deleted.



162
163
164
# File 'lib/cache_tree.rb', line 162

def clean
  diagnose[:dead].each { |file| FileUtils.rm file }
end

#debugObject

Prints out debugging information about this node.

Current cache file that exists in green Current cache file that is supposed to exist in yellow. All expired cache files are shown shown in red.



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cache_tree.rb', line 190

def debug
  diagnostic = diagnose
  template = "%-5s %5s %s"
  if File.exists?(diagnostic[:alive])
    puts template % ['+', 'current', diagnostic[:alive].to_ansi.green]
  else
    puts template % ['-', 'missing', diagnostic[:alive].to_ansi.yellow]
  end
  diagnostic[:dead].each_with_index do |file, index|
    puts template % [index + 1, 'expired', file.to_ansi.red]
  end
  nil
end

#diagnoseHash

Runs an analysis on a given node and returns its status.

node’s cache

Returns:

  • (Hash)

    with detailed diagnosis of curret status for that



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cache_tree.rb', line 170

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

#directoryObject



95
96
97
# File 'lib/cache_tree.rb', line 95

def directory
  self.class.directory
end

#expireObject

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.



151
152
153
154
# File 'lib/cache_tree.rb', line 151

def expire
  @stamp = generate_stamp
  save
end

#generate_stampObject



99
100
101
# File 'lib/cache_tree.rb', line 99

def generate_stamp
  (Time.now.to_f * 1.0).to_s + '-' + Kernel.rand(1000000).to_s
end

#initialize_from_hash(hash) ⇒ Object



85
86
87
88
# File 'lib/cache_tree.rb', line 85

def initialize_from_hash(hash)
  @name  = hash[:name]
  @value = hash[:value]
end

#initialize_from_object(target) ⇒ Object



90
91
92
93
# File 'lib/cache_tree.rb', line 90

def initialize_from_object(target)
  @name  = target.class.name.gsub(/([A-Z])/) { "_#{$1}" }.gsub(/^./, '').downcase
  @value = target.id
end

#loadObject

Updates current node stamp from btree_key



114
115
116
# File 'lib/cache_tree.rb', line 114

def load
  @stamp = File.read(btree_key).to_s
end

#load!Object

Updates current node stamp from btree_key ensuring the btree file is present reflecting node-data on memory.



120
121
122
# File 'lib/cache_tree.rb', line 120

def load!
  File.exists?(btree_key) ? load : save
end

#pathObject

Path of the cache node



104
105
106
# File 'lib/cache_tree.rb', line 104

def path
  "#{name}#{SEPARATOR}#{value}#{SEPARATOR}"
end

#saveObject

Writes node stamp from to btree_key file



143
144
145
146
# File 'lib/cache_tree.rb', line 143

def save
  FileUtils.mkdir_p File.dirname(btree_key)
  File.open(btree_key, 'w+') { |file| file.write stamp }
end