Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@watsonbox
Created July 24, 2012 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save watsonbox/3170415 to your computer and use it in GitHub Desktop.
Save watsonbox/3170415 to your computer and use it in GitHub Desktop.
PHP Memcache Session Store Using Dalli
require 'action_dispatch/middleware/session/dalli_store'
# - PHP side is 5.3.6 configured with session.save_handler = memcache
# - Requires php-serialize gem for serialization: https://github.com/jqr/php-serialize
module ActionDispatch
module Session
class PhpDalliStore < ActionDispatch::Session::DalliStore
require 'php_serialize'
class PhpSerializingPoolDecorator < SimpleDelegator
def set(key, value, ttl = nil, options = nil)
super(key, PHP.serialize_session(value), 0, :raw => true)
end
def get(key, options = nil)
value = super(key)
PHP.unserialize(value) if value && value != ''
end
end
def initialize(app, options = {})
# Default to no namespace and cookie name matching PHP settings
super(app, options.merge(:namespace => nil, :key => 'PHPSESSID'))
# Decorate the pool to do some serialization
@pool = PhpSerializingPoolDecorator.new(@pool)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment