cache_tree 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,21 @@
1
- = cache_tree
1
+ = CacheTree
2
2
 
3
- Btree based cache
3
+ The idea behind this, is to be able to manage very large ammounts of cache files.
4
+ and expire millions of cache files in just a few microseconds.
5
+
6
+ Instead of performing some ActiveRecord#touch and have it creating a massive sequence
7
+ of queries bouncing off going up/down the herarchies to eliminate a group of items from the cache
8
+
9
+ CacheTree stores cache in trees, actually it borrows the btree strategy to fetch
10
+ and quickly determine where cache files are.
11
+
12
+ you can always add new subtrees or create new trees and handle the cache at different levels.
13
+ no matter where you are cache expiring is superfast.
14
+
15
+ While the actual deletion of the expired cache files is also fast. it is recomended to do so
16
+ in some sort of background job solution.
17
+
18
+ Trees are cool :)
4
19
 
5
20
  == Contributing to cache_tree
6
21
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/cache_tree.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cache_tree}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["kazuyoshi tlacaelel"]
12
- s.date = %q{2012-11-01}
12
+ s.date = %q{2012-11-02}
13
13
  s.description = %q{Cache based on btrees, for large ammounts of cache.}
14
14
  s.email = %q{kazu.dev@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/cache_tree.rb CHANGED
@@ -6,6 +6,14 @@ require 'tree_node'
6
6
 
7
7
  module CacheTree
8
8
 
9
+ def self.use(node, report = proc {}, &block)
10
+ CacheTree::Manager.instance.use(node, report, &block)
11
+ end
12
+
13
+ def self.node(object)
14
+ CacheTree::Node.new(object)
15
+ end
16
+
9
17
  class Manager
10
18
 
11
19
  include Singleton
@@ -17,31 +25,33 @@ module CacheTree
17
25
  @configuration = { :perform_caching => true }
18
26
  end
19
27
 
20
- def use(cache_link)
21
- @current_link = cache_link
28
+ def use(node, report, &block)
29
+ return yield unless @configuration[:perform_caching]
30
+ @node = node
31
+ @node.map(:up) { |parent| parent.load! }
32
+ return read if exists?
33
+ report.call
34
+ save(yield)
22
35
  end
23
36
 
37
+ protected
38
+
24
39
  def exists?
25
40
  return false unless @configuration[:perform_caching]
26
- btree_key_files = @current_link.map(:up) { |node| node.btree_key }
41
+ btree_key_files = @node.map(:up) { |node| node.btree_key }
27
42
  return false unless btree_key_files.all? { |btree_key| File.exists?(btree_key) }
28
- @current_link.map(:up) { |node| node.load_btree_key }
29
- File.exists?(@current_link.cache_file)
30
- end
31
-
32
- def expire!
33
- @current_link.expire
43
+ File.exists?(@node.cache_file)
34
44
  end
35
45
 
36
46
  def read
37
- @current_link.map(:up) { |node| node.load_btree_key }
38
- File.read(@current_link.cache_file)
47
+ File.read(@node.cache_file)
39
48
  end
40
49
 
41
50
  def save(data)
42
- @current_link.map(:up) { |node| node.save }
43
- FileUtils.mkdir_p(File.dirname(@current_link.cache_file))
44
- File.open(@current_link.cache_file, 'w+') { |file| file.write data }
51
+ @node.map(:up) { |node| node.save }
52
+ FileUtils.mkdir_p(File.dirname(@node.cache_file))
53
+ File.open(@node.cache_file, 'w+') { |file| file.write data }
54
+ data
45
55
  end
46
56
 
47
57
  end
@@ -83,10 +93,16 @@ module CacheTree
83
93
  end
84
94
 
85
95
  # Updates current node stamp from btree_key
86
- def load_btree_key
96
+ def load
87
97
  @stamp = eval(File.read(btree_key))
88
98
  end
89
99
 
100
+ # Updates current node stamp from btree_key
101
+ # ensuring the btree file is present reflecting node-data on memory.
102
+ def load!
103
+ File.exists?(btree_key) ? load : save
104
+ end
105
+
90
106
  # Sums up all stamps and generates a checksum.
91
107
  def checksum
92
108
  MD5.hexdigest(map(:up) { |node| node.stamp } * '')
@@ -126,11 +142,11 @@ module CacheTree
126
142
  # @return [Hash] with detailed diagnosis of curret status for that
127
143
  # node's cache
128
144
  def diagnose
145
+ map(:up) { |node| node.load! }
129
146
  file_alive = cache_file
130
147
  base = File.basename(file_alive)
131
148
  dir = File.dirname(file_alive)
132
149
  diagnostic = {}
133
- diagnostic[:btree_key] = btree_key
134
150
  diagnostic[:alive] = file_alive
135
151
  diagnostic[:dead] = []
136
152
  Dir["#{dir}/*.cache"].each do |cached_file|
@@ -142,11 +158,17 @@ module CacheTree
142
158
 
143
159
  # Prints out active cache in green, and expired files in red.
144
160
  def debug
145
- puts cache_file.to_ansi.green
146
- diagnose[:dead].each { |file| puts file.to_ansi.red }
161
+ diagnostic = diagnose
162
+ if File.exists?(diagnostic[:alive])
163
+ puts diagnostic[:alive].to_ansi.green
164
+ else
165
+ puts diagnostic[:alive].to_ansi.yellow
166
+ end
167
+ diagnostic[:dead].each { |file| puts file.to_ansi.red }
147
168
  nil
148
169
  end
149
170
 
171
+
150
172
  end
151
173
 
152
174
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_tree
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - kazuyoshi tlacaelel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-11-01 00:00:00 -06:00
18
+ date: 2012-11-02 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency