-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from patriciomacadden/dalli
Add a recipe about using Dalli for sessions stored in memcached
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# How to share a session with Memcached? | ||
|
||
Suppose that you are developing a Sinatra app that will be deployed to multiple | ||
server instances and must be seen as a single application (it must be balanced | ||
with nginx, for instance). You need a mechanism for sharing user sessions | ||
between all the application instances. To accomplish this task, we'll use | ||
memcached for sharing the session and [Dalli](https://github.com/mperham/dalli) | ||
as the memcached client. | ||
|
||
First, add `dalli` to your `Gemfile`: | ||
|
||
```ruby | ||
gem 'dalli' | ||
``` | ||
|
||
Then, add the `Rack::Session::Dalli` middleware: | ||
|
||
```ruby | ||
configure do | ||
use Rack::Session::Dalli, cache: Dalli::Client.new | ||
end | ||
``` | ||
|
||
`Dalli::Client` defaults the memcache server url to `localhost:11211`. If you | ||
need to use another server, you must configure it yourself. | ||
|
||
See a full example configuring the memcache server and the session namespace: | ||
|
||
```ruby | ||
configure do | ||
use Rack::Session::Dalli, | ||
memcache_server: 'example.com:11211', | ||
namespace: 'other.namespace' | ||
cache: Dalli::Client.new | ||
end | ||
``` | ||
|
||
## More Info | ||
|
||
Refer to [Rack::Session](http://rack.rubyforge.org/doc/Rack/Session.html) | ||
documentation to know more about rack sessions. | ||
Refer to [Dalli](https://github.com/mperham/dalli) documentation to know more | ||
about Rack::Session::Dalli. |