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.



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

.directoryObject

Returns the value of attribute directory.



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

def directory
  @directory
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#stampObject

Returns the value of attribute stamp.



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

def stamp
  @stamp
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#btree_keyObject

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_fileObject

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

#checksumObject

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

#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.



136
137
138
# File 'lib/cache_tree.rb', line 136

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

#debugObject

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

#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



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

#directoryObject



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

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.



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

def expire
  @stamp = generate_stamp
  save
end

#generate_stampObject



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

#loadObject

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

#pathObject

Path of the cache node



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

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

#saveObject

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