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

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



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

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

#cache_fileObject

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

#checksumObject

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

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



149
150
151
# File 'lib/cache_tree.rb', line 149

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

#debugObject

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

#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



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

#directoryObject



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

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.



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

def expire
  @stamp = generate_stamp
  save
end

#generate_stampObject



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

#loadObject

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

#pathObject

Path of the cache node



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

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

#saveObject

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