-
-
Save timmillwood/3986324 to your computer and use it in GitHub Desktop.
the simplest Redis caching for Sinatra -- not even tested... consider this code just an hint
This file contains hidden or 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
Username = <%= @var %> |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Sinatra Redis Cache Example</title> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> |
This file contains hidden or 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 is just an hint for a simple Redis caching "framework" using Sinatra | |
# A friend of mine asked for something like that, after checking the first two gems realized there was | |
# a lot of useless complexity (IMHO). That's the result. | |
# | |
# The use_cache parameter is useful if you want to cache only if the user is authenticated or | |
# something like that, so you can do cache_url(ttl,User.logged_in?, ...) | |
require 'rubygems' | |
require 'sinatra' | |
require 'redis' | |
configure do | |
uri = URI.parse(ENV["REDISTOGO_URL"]) | |
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password) | |
end | |
helpers do | |
def cache(tag,ttl,use_cache,block) | |
page = REDIS.get(tag) | |
if page and use_cache and !logged_in? | |
response.header['redis'] = 'HIT' | |
return page | |
else | |
page = block.call | |
REDIS.setex(tag,ttl,page) if use_cache and !logged_in? | |
response.header['redis'] = 'MISS' | |
return page | |
end | |
end | |
def cache_url(ttl=300,use_cache=true,&block) | |
cache("url:#{request.url}",ttl,use_cache,block) | |
end | |
end | |
get '/foo/:username' do | |
@var = "Hello #{params[:username]}" | |
cache_url(10,true) {erb :index} | |
end |
This file contains hidden or 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
Username = <%= @var %> |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Sinatra Redis Cache Example</title> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment