Skip to content

Commit

Permalink
Missing files for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Herrmann committed Jan 16, 2009
1 parent aee8e29 commit 805f635
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
12 changes: 0 additions & 12 deletions config/init.rb
Expand Up @@ -24,12 +24,6 @@
# c[:session_id_key] = '_session_id' # cookie session id key, defaults to "_session_id"
end

dependency "merb-cache" do
Merb::Cache.setup do
register(Merb::Cache::ActionStore[Merb::Cache::FileStore])
end
end

Merb::BootLoader.before_app_loads do
Merb.push_path(:merb_extensions, Merb.root / "merb/extensions", "**/*.rb")
Merb.push_path(:lib_wikipedia, Merb.root / "lib" / "wikipedia", "**/*.rb")
Expand All @@ -43,16 +37,10 @@
Device.available_formats = YAML::load(open("config/formats.yaml"))
rescue Exception => e
puts "There appears to be a syntax error in your YAML configuration files."
raise
exit
end
end

#Merb::Plugins.config[:merb_cache] = {
# :store => "memory"
#}


# Add our mime-types for device based content type negotiation
%w[webkit_native webkit wml].each do |type|
Merb.add_mime_type(:"#{type}", :to_html, %w[text/html])
Expand Down
28 changes: 28 additions & 0 deletions lib/cache.rb
@@ -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
25 changes: 25 additions & 0 deletions spec/lib/cache_spec.rb
@@ -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

0 comments on commit 805f635

Please sign in to comment.