Skip to content

Commit

Permalink
Merge pull request #64 from patriciomacadden/dalli
Browse files Browse the repository at this point in the history
Add a recipe about using Dalli for sessions stored in memcached
  • Loading branch information
patriciomacadden committed Aug 12, 2013
2 parents ccbc86c + b359eb5 commit ac5b5a4
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions middleware/share_session_with_memcached.md
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.

0 comments on commit ac5b5a4

Please sign in to comment.