File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ And that's all, from now on we'll be storing the users sessions in our local
25
+ memcached server (` localhost:11211 ` ) and the ` rack:session ` namespace. If you
26
+ want to change these settings, use the middleware like this:
27
+
28
+ ``` ruby
29
+ configure do
30
+ use Rack ::Session ::Dalli ,
31
+ memcache_server: ' example.com:11211' ,
32
+ namespace: ' other.namespace'
33
+ cache: Dalli ::Client .new
34
+ end
35
+ ```
36
+
37
+ If you need more info, refer to the [ dalli gem documentation] ( https://github.com/mperham/dalli ) .
You can’t perform that action at this time.
0 commit comments