方法1:
If you use the full stack Symfony framework, than configuring memcached as a session handler is as simple as specifying it in your php.ini:
session.save_handler=memcached
session.save_path=localhost:11211
Make sure that the handler_id
is set to null (~
) in your app/config/config.yml
. This way Symfony will use the native php handler:
framework:
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
Now you can start using your session. It is going to be stored in memcached.
Accessing the session
Session can be accessed from the Request
object:
$request->getSession()->set('name', 'Kuba');
HttpFoundation component
The same can be set up outside of a full stack framework with the HttpFoundation component alone:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
$storage = new NativeSessionStorage(array());
$session = new Session($storage, new NamespacedAttributeBag());
$request = Request::createFromGlobals();
$request->setSession($session);
The above snippet sets the session up just like the full stack framework does (by default). This way, the script you'll put this code in, will share the session with a full Symfony application.
方法2:https://gist.github.com/K-Phoen/4327229
Storing Symfony2 sessions in memcached
imports: | |
# .... | |
- { resource: services/session.yml } | |
framework: | |
# .... | |
session: | |
handler_id: session.handler.memcached |
aptitude install memcached php5-memcached |
parameters: | |
# ... | |
session_memcached_host: localhost | |
session_memcached_port: 11211 | |
session_memcached_prefix: sess | |
session_memcached_expire: 3600 |
services: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
session.memcached: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class: Memcached | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
arguments: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
persistent_id: %session_memcached_prefix% | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
calls: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- [ addServer, [ %session_memcached_host%, %session_memcached_port% ]] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
session.handler.memcached: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
arguments: [@session.memcached, { prefix: %session_memcached_prefix%, expiretime: %session_memcached_expire% }]
方法3:https://gist.github.com/benr77/258e42642b4632d5a826#file-memcachedwrapper-php Memcached wrapper for persistent connections
方法4:https://gist.github.com/elmariachi111/5637669a028c29e3973a Symfony2 Memcached settings for persistent connections
adamquaile commented on 8 Jul 2015 I know this is old, but for anyone stumbling across this. Just be aware there is also a singular benr77 commented on 29 Jul 2015 I've added the method for addServer as well - see my Gist based on this one. https://gist.github.com/benr77/258e42642b4632d5a826#file-memcachedwrapper-php
|