-
1
class Duality
-
-
1
VERSION = "0.0.5"
-
1
@@fast, @@slow = nil
-
-
1
def initialize fast, slow
-
# check params are caches
-
10
raise NotACache unless fast.respond_to?(:set) && fast.respond_to?(:get)
-
9
raise NotACache unless slow.respond_to?(:set) && slow.respond_to?(:get)
-
9
@fast = fast
-
9
@slow = slow
-
end
-
-
# Set to fast and slow.
-
# - return true if both succeed
-
# - return false if either fail
-
1
def set key, value
-
10
run_method(:set, key, value)
-
end
-
1
alias :save :set
-
-
# Delete from both - async
-
1
def delete key
-
1
run_method(:delete, key)
-
end
-
1
alias :remove :delete
-
-
# Flush caches - async
-
1
def flush
-
1
run_method(:flush)
-
end
-
1
alias :clean :flush
-
-
1
def flush_expired
-
1
run_method(:flush_expired)
-
end
-
1
alias :cleanup :flush_expired
-
-
# Get from fast or slow.
-
# - returns nil if none are found
-
1
def get key
-
3
content = nil
-
3
begin
-
3
content = @fast.get(key)
-
rescue Exception
-
end
-
-
3
return content unless content.nil?
-
-
1
begin
-
1
content = @slow.get(key)
-
rescue Exception
-
end
-
1
return content
-
end
-
1
alias :load :get
-
-
# Add support for other methods from passed caches
-
# this adds support only, but no speed gains.
-
# - use "strict_" to ensure both caches contain method
-
1
def method_missing(meth, *args, &block)
-
2
if meth =~ /^strict_(.+)$/
-
1
meth = $1
-
1
return run_missing_method(meth, *args, &block) if @fast.respond_to?(meth) && @slow.respond_to?(meth)
-
elsif @fast.respond_to?(meth) || @slow.respond_to?(meth)
-
1
return run_missing_method(meth, *args, &block)
-
end
-
1
super
-
end
-
-
1
private
-
1
def run_missing_method meth, *args, &block
-
1
fast = @fast.send(meth, *args, &block) rescue nil
-
1
slow = @slow.send(meth, *args, &block) rescue nil
-
1
return fast if fast == slow
-
1
return slow if fast.nil?
-
1
return fast
-
end
-
-
1
def run_method meth, *args
-
13
success = true
-
13
begin
-
13
@fast.send(meth, *args)
-
rescue Exception
-
success = false
-
rescue
-
success = false
-
end
-
13
begin
-
13
@slow.send(meth, *args)
-
rescue Exception
-
success = false
-
rescue
-
success = false
-
end
-
13
return success
-
end
-
-
1
class NotACache < Exception
-
end
-
-
1
class CacheTimeout < Exception
-
end
-
end