forked from HamptonMakes/wikimedia-mobile
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jens Herrmann
committed
Jan 16, 2009
1 parent
aee8e29
commit 805f635
Showing
3 changed files
with
53 additions
and
12 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Simple cache for in memory caching of rendered pages | ||
|
||
class Cache | ||
@@_cache= {} | ||
|
||
def self.read(key) | ||
return unless @@_cache[key] | ||
if @@_cache[key][:expires].nil? || @@_cache[key][:expires]>Time.now | ||
@@_cache[key][:data] | ||
else | ||
self.expire(key) | ||
end | ||
end | ||
|
||
def self.write(key, data, options={}) | ||
@@_cache[key] ||= {} | ||
@@_cache[key][:expires]= options[:expires] | ||
@@_cache[key][:data]= data | ||
end | ||
|
||
def self.expire(key) | ||
@@_cache[key]= nil | ||
end | ||
|
||
def self.dump | ||
puts @@_cache.keys.map{|key| [key, @@_cache[key][:expires], @@_cache[key][:data][0..100]]}.inspect | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb') | ||
|
||
describe Cache do | ||
|
||
it "should cache a value" do | ||
Cache.read(:key).should be_nil | ||
data="Little thing in cache" | ||
Cache.write(:key, data) | ||
Cache.read(:key).should ==data | ||
end | ||
|
||
it "should expire a cached value" do | ||
Cache.expire(:key) | ||
Cache.read(:key).should be_nil | ||
end | ||
|
||
it "should expire a cached value after a certain time" do | ||
data="Little thing in cache" | ||
Cache.write(:key, data, :expires=>Time.now+2) #seconds | ||
Cache.read(:key).should ==data | ||
sleep 2 | ||
Cache.read(:key).should be_nil | ||
end | ||
end | ||
|