Last active
June 8, 2020 10:31
-
-
Save rkjha/1e456a2573d25ba150501b576578630a to your computer and use it in GitHub Desktop.
Simple use of Dalli gem to implement caching using memcached in a Sinatra Application.
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
require 'rubygems' | |
require 'sinatra/base' | |
require 'sinatra/content_for' | |
require 'dalli' | |
class MyBlog < Sinatra::Base | |
helpers Sinatra::ContentFor | |
set :dc, Dalli::Client.new('localhost:11211') | |
get '/articles/:id' do | |
# .............. | |
post_id = params[:id] | |
key = "post_#{post_id}" | |
@article = settings.dc.get(key) || set_cache(key, Article.find(post_id)) | |
# .............. | |
end | |
def set_cache key, value | |
settings.dc.set(key, value) | |
return value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment