duality 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/duality.rb +48 -15
- metadata +1 -1
data/lib/duality.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'timeout'
|
2
2
|
class Duality
|
3
3
|
|
4
|
-
VERSION = "0.0.
|
5
|
-
DEFAULT_TIMEOUT=1
|
4
|
+
VERSION = "0.0.2"
|
5
|
+
#DEFAULT_TIMEOUT=1
|
6
6
|
@@fast, @@slow = nil
|
7
7
|
|
8
8
|
# Used to set cache connection timeout
|
9
9
|
# - default is 0.5 seconds
|
10
|
-
attr_accessor :timeout
|
10
|
+
#attr_accessor :timeout
|
11
11
|
|
12
12
|
def initialize fast, slow
|
13
13
|
# check params are caches
|
@@ -15,28 +15,60 @@ class Duality
|
|
15
15
|
raise NotACache unless slow.respond_to?(:set) && slow.respond_to?(:get)
|
16
16
|
@fast = fast
|
17
17
|
@slow = slow
|
18
|
+
Thread.abort_on_exception = true
|
18
19
|
end
|
19
20
|
|
20
21
|
# Return set timeout or use default.
|
21
|
-
def timeout
|
22
|
-
|
23
|
-
end
|
22
|
+
#def timeout
|
23
|
+
#@timeout||DEFAULT_TIMEOUT
|
24
|
+
#end
|
24
25
|
|
25
26
|
# Set to fast and slow.
|
26
|
-
# - timeout after #timeout
|
27
27
|
# - return true if both succeed
|
28
28
|
# - return false if either fail
|
29
29
|
def set key, value
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
30
|
+
fast = Thread.new { @fast.set(key, value) }
|
31
|
+
slow = Thread.new { @slow.set(key, value) }
|
32
|
+
|
33
|
+
timeout = 10 # iterations, or 1 seconds
|
34
|
+
current = 0
|
35
|
+
while (fast.alive? || slow.alive?) do
|
36
|
+
raise CacheTimeout if current == timeout
|
37
|
+
sleep 0.1
|
38
|
+
current = current + 1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
alias :save :set
|
42
|
+
|
43
|
+
# Delete from both - async
|
44
|
+
def delete key
|
45
|
+
fast = Thread.new { @fast.delete(key) }
|
46
|
+
slow = Thread.new { @slow.delete(key) }
|
47
|
+
|
48
|
+
timeout = 10 # iterations, or 1 seconds
|
49
|
+
current = 0
|
50
|
+
while (fast.alive? || slow.alive?) do
|
51
|
+
raise CacheTimeout if current == timeout
|
52
|
+
sleep 0.1
|
53
|
+
current = current + 1
|
54
|
+
end
|
55
|
+
end
|
56
|
+
alias :remove :delete
|
57
|
+
|
58
|
+
# Flush caches - async
|
59
|
+
def flush
|
60
|
+
fast = Thread.new { @fast.flush }
|
61
|
+
slow = Thread.new { @slow.flush }
|
62
|
+
|
63
|
+
timeout = 10 # iterations, or 1 seconds
|
64
|
+
current = 0
|
65
|
+
while (fast.alive? || slow.alive?) do
|
66
|
+
raise CacheTimeout if current == timeout
|
67
|
+
sleep 0.1
|
68
|
+
current = current + 1
|
38
69
|
end
|
39
70
|
end
|
71
|
+
alias :clean :flush
|
40
72
|
|
41
73
|
# Get from fast or slow.
|
42
74
|
# - returns nil if none are found
|
@@ -55,6 +87,7 @@ class Duality
|
|
55
87
|
end
|
56
88
|
return content
|
57
89
|
end
|
90
|
+
alias :load :get
|
58
91
|
|
59
92
|
# Add support for other methods from passed caches
|
60
93
|
# this adds support only, but no speed gains.
|