elephant-cache 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -1
- data/lib/elephant.rb +27 -0
- data/lib/elephant/cache.rb +7 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -23,7 +23,7 @@ Example:
|
|
23
23
|
attr_reader :x, :y
|
24
24
|
|
25
25
|
def magnitude
|
26
|
-
return
|
26
|
+
return cache_value(__method__) do
|
27
27
|
Math.sqrt(self.x**2 + self.y**2)
|
28
28
|
end
|
29
29
|
end
|
@@ -60,4 +60,7 @@ or directly via e-mail at:
|
|
60
60
|
mailto:jeff@paploo.net
|
61
61
|
|
62
62
|
= Version History
|
63
|
+
[1.0.1 - 2011-Sep-01] Added timing.
|
64
|
+
* (FEATURE) Added the ability to turn on cache calculation timing output.
|
65
|
+
* (FIX) Some documentation referred to +cache_value+ as +cache+.
|
63
66
|
[1.0.0 - 2011-Aug-28] Initial release.
|
data/lib/elephant.rb
CHANGED
@@ -1 +1,28 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'elephant', 'cache')
|
2
|
+
|
3
|
+
module Elephant
|
4
|
+
|
5
|
+
@@time = false
|
6
|
+
@@time_io = STDOUT
|
7
|
+
|
8
|
+
# Used by the caches to determine if they should report the time they took to process.
|
9
|
+
def self.time?
|
10
|
+
return @@time ? true : false
|
11
|
+
end
|
12
|
+
|
13
|
+
# If set to true, the caches will report the time to process when they process.
|
14
|
+
def self.time=(flag)
|
15
|
+
@@time = flag ? true : false
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns the IO object that timing will puts to.
|
19
|
+
def self.time_io
|
20
|
+
return @@time_io
|
21
|
+
end
|
22
|
+
|
23
|
+
# Sets the IO object that timing will puts to.
|
24
|
+
def self.time_io=(time_io)
|
25
|
+
@@time_io = time_io
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/lib/elephant/cache.rb
CHANGED
@@ -33,7 +33,14 @@ module Elephant
|
|
33
33
|
# end
|
34
34
|
def cache_value(key)
|
35
35
|
unless(@elephant_cache.include?(key))
|
36
|
+
start_at = Time.now
|
36
37
|
@elephant_cache[key] = yield if block_given?
|
38
|
+
stop_at = Time.now
|
39
|
+
delta_t = stop_at - start_at
|
40
|
+
if( Elephant.time? )
|
41
|
+
label = "#{self.class.name} - #{key}"
|
42
|
+
Elephant.time_io.puts " -- #{label.to_s.rjust(32)}: #{('%0.3f' % (delta_t*1000.0)).rjust(9)} ms (#{caller[0]})"
|
43
|
+
end
|
37
44
|
end
|
38
45
|
return @elephant_cache[key]
|
39
46
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: elephant-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0.
|
5
|
+
version: 1.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jeff Reinecke
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-09-02 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: " A simple object caching design pattern in a reusable module.\n"
|