Created
February 5, 2010 14:50
-
-
Save happymcplaksin/295842 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/tdcache.rb b/lib/tdcache.rb | |
index a2a295e..767e007 100644 | |
--- a/lib/tdcache.rb | |
+++ b/lib/tdcache.rb | |
@@ -37,6 +37,15 @@ module TDcache | |
run_or_not(file){|file,cachefile| TDcache.file(file,cachefile) } | |
end | |
+ def self.give_url(url) | |
+ run_or_not(url){|url,cachefile| TDcache.url(url,cachefile) } | |
+ end | |
+ | |
+ def self.give_code(a) | |
+ run_or_not(a){|proc,cachefile| TDcache.code(proc,cachefile) } | |
+ end | |
+ | |
+ | |
# get the cache directory | |
def self.dir | |
self.instance_variable_get("@dir") | |
@@ -80,8 +89,15 @@ module TDcache | |
end | |
private | |
def self.run_or_not(command) | |
- # look up the cache of command | |
- cachefile = TDcache.cachename(command) | |
+ # Yup, it's a hack! | |
+ proc = nil | |
+ if command.class == Array | |
+ proc = command.last | |
+ cachefile = command = TDcache.cachename(command.first) | |
+ else | |
+ # look up the cache of command | |
+ cachefile = TDcache.cachename(command) | |
+ end | |
if self.instance_variable_get("@cache_only") | |
p "reading from #{self.instance_variable_get("@dir")} cache for [#{command}] because of cache_only" if @debug | |
return TDcache.readcache(cachefile) | |
@@ -89,6 +105,11 @@ module TDcache | |
# check caching option | |
if TDcache.do_cache? | |
c = command.firstword | |
+ # Further hack | |
+ if proc | |
+ command = proc | |
+ proc_on = true | |
+ end | |
# look up TTL for command | |
ttl = @ttls[c] | |
life = 0 | |
@@ -101,6 +122,11 @@ module TDcache | |
# read the cache | |
p "reading from #{self.instance_variable_get("@dir")} cache for [#{command}]" if @debug | |
out = TDcache.readcache(cachefile) | |
+ # Hack #3 | |
+ if proc_on | |
+ out = Marshal.load(out) | |
+ end | |
+ out | |
end | |
else | |
out = yield command,cachefile | |
@@ -139,6 +165,18 @@ module TDcache | |
self.writecache(cachefile,out) | |
out | |
end | |
+ def self.url(url,cachefile) | |
+ p "fetching ACTUAL URL #{url}" if @debug | |
+ out = Net::HTTP.get_response(URI.parse(url)).body | |
+ self.writecache(cachefile,out) | |
+ out | |
+ end | |
+ def self.code(proc,cachefile) | |
+ p "Running ACTUAL code" if @debug | |
+ out = proc.call | |
+ self.writecache(cachefile,Marshal.dump(out)) | |
+ out | |
+ end | |
def self.decode(str) | |
[str.sub(/.*\//,'')].pack("H*") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment