Skip to content

Commit ac5b5a4

Browse files
Merge pull request #64 from patriciomacadden/dalli
Add a recipe about using Dalli for sessions stored in memcached
2 parents ccbc86c + b359eb5 commit ac5b5a4

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# How to share a session with Memcached?
2+
3+
Suppose that you are developing a Sinatra app that will be deployed to multiple
4+
server instances and must be seen as a single application (it must be balanced
5+
with nginx, for instance). You need a mechanism for sharing user sessions
6+
between all the application instances. To accomplish this task, we'll use
7+
memcached for sharing the session and [Dalli](https://github.com/mperham/dalli)
8+
as the memcached client.
9+
10+
First, add `dalli` to your `Gemfile`:
11+
12+
```ruby
13+
gem 'dalli'
14+
```
15+
16+
Then, add the `Rack::Session::Dalli` middleware:
17+
18+
```ruby
19+
configure do
20+
use Rack::Session::Dalli, cache: Dalli::Client.new
21+
end
22+
```
23+
24+
`Dalli::Client` defaults the memcache server url to `localhost:11211`. If you
25+
need to use another server, you must configure it yourself.
26+
27+
See a full example configuring the memcache server and the session namespace:
28+
29+
```ruby
30+
configure do
31+
use Rack::Session::Dalli,
32+
memcache_server: 'example.com:11211',
33+
namespace: 'other.namespace'
34+
cache: Dalli::Client.new
35+
end
36+
```
37+
38+
## More Info
39+
40+
Refer to [Rack::Session](http://rack.rubyforge.org/doc/Rack/Session.html)
41+
documentation to know more about rack sessions.
42+
Refer to [Dalli](https://github.com/mperham/dalli) documentation to know more
43+
about Rack::Session::Dalli.

0 commit comments

Comments
 (0)