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.



61
62
63
64
65
# File 'lib/cache_tree.rb', line 61

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

Class Attribute Details

.directoryObject

Returns the value of attribute directory.



52
53
54
# File 'lib/cache_tree.rb', line 52

def directory
  @directory
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



57
58
59
# File 'lib/cache_tree.rb', line 57

def name
  @name
end

#stampObject

Returns the value of attribute stamp.



57
58
59
# File 'lib/cache_tree.rb', line 57

def stamp
  @stamp
end

#valueObject

Returns the value of attribute value.



57
58
59
# File 'lib/cache_tree.rb', line 57

def value
  @value
end

Instance Method Details

#btree_keyObject

Resolves final name for btree_key file.



81
82
83
# File 'lib/cache_tree.rb', line 81

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

#cache_fileObject

Resolves final name for cache file.



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

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

#checksumObject

Sums up all stamps and generates a checksum.



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

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.



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

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

#debugObject

Prints out active cache in green, and expired files in red.



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

def debug
  puts cache_file.to_ansi.green
  diagnose[:dead].each { |file| puts file.to_ansi.red }
  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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cache_tree.rb', line 128

def diagnose
  file_alive             = cache_file
  base                   = File.basename(file_alive)
  dir                    = File.dirname(file_alive)
  diagnostic             = {}
  diagnostic[:btree_key] = btree_key
  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



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

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.



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

def expire
  @stamp = generate_stamp
  save
end

#generate_stampObject



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

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

#load_btree_keyObject

Updates current node stamp from btree_key



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

def load_btree_key
  @stamp = eval(File.read(btree_key))
end

#pathObject

Path of the cache node



76
77
78
# File 'lib/cache_tree.rb', line 76

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

#saveObject

Writes node stamp from to btree_key file



101
102
103
104
# File 'lib/cache_tree.rb', line 101

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