Created
February 9, 2011 13:31
-
-
Save anonymous/818473 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
session.storage.memcached: | |
class: MyApp\MyBundle\Session\MemcachedSessionStorage | |
arguments: | |
- @memcached | |
- %session.storage.memcached.options% | |
session.registryStorage.memcached: | |
class: MyApp\MyBundle\Session\MemcachedSessionRegistryStorage | |
arguments: | |
- @memcached | |
security.authentication.session_strategy: | |
class: Symfony\Component\Security\Http\Session\ConcurrentSessionControlStrategy | |
arguments: | |
- %security.authentication.session.maximum% | |
- @security.authentication.session_strategy_registry | |
- %security.authentication.session_strategy.strategy% | |
security.authentication.session_strategy_registry: | |
class: MyApp\MyBundle\Session\SessionRegistry | |
arguments: | |
- @session.registryStorage.memcached | |
- @session.storage.memcached | |
memcached: | |
class: Memcached | |
calls: | |
- [addServer, [localhost, 11211]] | |
parameters: | |
session.storage.memcached.options: [] | |
security.authentication.session.maximum: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyApp\MyBundle\Session; | |
use Symfony\Component\Security\Core\User\AccountInterface; | |
class MemcachedSessionRegistryStorage implements SessionRegistryStorageInterface | |
{ | |
protected $memcached; | |
public function __construct(\Memcached $memcached) | |
{ | |
$this->memcached = $memcached; | |
} | |
public function read($user) | |
{ | |
return unserialize($this->memcached->get($this->createIdentifier($user))); | |
} | |
public function add($sessionId, $user) | |
{ | |
$sessionIds = $this->read($user); | |
if (!is_array($sessionIds)) { | |
$sessionIds = array(); | |
} | |
$sessionIds[] = $sessionId; | |
$this->memcached->set($this->createIdentifier($user), serialize($sessionIds)); | |
} | |
public function remove($sessionId, $user) | |
{ | |
$sessionIds = $this->read($user); | |
if (is_array($sessionIds)) { | |
$sessionIds = array_diff($sessionIds, array($sessionId)); | |
$this->memcached->set($this->createIdentifier($user), serialize(array_values($sessionIds))); | |
} | |
} | |
protected function createIdentifier($user) | |
{ | |
if ($user instanceof AccountInterface) { | |
return get_class($user).':'.$user->getUsername(); | |
} | |
return (is_object($user) ? get_class($user) : 'STRING').':'.(string) $user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyApp\MyBundle\Session; | |
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage; | |
use Symfony\Component\HttpFoundation\SessionStorage\ManageableSessionStorageInterface; | |
/** | |
* NativeSessionStorage. | |
* | |
* @author | |
*/ | |
class MemcachedSessionStorage extends NativeSessionStorage implements ManageableSessionStorageInterface | |
{ | |
protected $memcached; | |
public function __construct(\Memcached $memcached, array $options = array()) | |
{ | |
$this->memcached = $memcached; | |
session_set_save_handler(array($this, 'storageOpen'), array($this, 'storageClose'), array($this, 'storageRead'), array($this, 'storageWrite') , array($this, 'storageRemove'), array($this, 'storageGc')); | |
parent::__construct($options); | |
} | |
public function storageOpen($path, $name) | |
{ | |
return true; | |
} | |
public function storageClose() | |
{ | |
return true; | |
} | |
public function storageRead($id) | |
{ | |
if (($data = unserialize($this->memcached->get($this->generateKey($id)))) && (is_array($data)) && (array_key_exists('session', $data))) { | |
return $data['session']; | |
} | |
return false; | |
} | |
public function storageWrite($id, $sessionData) | |
{ | |
$data = array('modified' => $_SERVER['REQUEST_TIME'], 'session' => $sessionData); | |
return $this->memcached->set($this->generateKey($id), serialize($data)); | |
} | |
public function storageRemove($id) | |
{ | |
return $this->memcached->delete($this->generateKey($id)); | |
} | |
public function storageGc($maxlifetime) | |
{ | |
} | |
public function getModifiedDate($id) | |
{ | |
if (($data = unserialize($this->memcached->get($this->generateKey($id)))) && (is_array($data)) && (array_key_exists('modified', $data))) { | |
return $data['modified']; | |
} | |
return false; | |
} | |
protected function generateKey($key) | |
{ | |
return $key; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyApp\MyBundle\Session; | |
use Symfony\Component\Security\Http\Session\SessionRegistryInterface; | |
use Symfony\Component\HttpFoundation\SessionStorage\ManageableSessionStorageInterface; | |
class SessionRegistry implements SessionRegistryInterface | |
{ | |
protected $sessionRegistryStorage; | |
protected $sessionStorage; | |
public function __construct(SessionRegistryStorageInterface $sessionRegistryStorage, ManageableSessionStorageInterface $sessionStorage) | |
{ | |
$this->sessionRegistryStorage = $sessionRegistryStorage; | |
$this->sessionStorage = $sessionStorage; | |
} | |
public function getAllUsers() | |
{ | |
return false; | |
} | |
public function getAllSessions($user, $includeExpiredSessions = false) | |
{ | |
$sessions = array(); | |
$sessionIds = $this->sessionRegistryStorage->read($user); | |
if (is_array($sessionIds)) { | |
foreach ($sessionIds as $sessionId) { | |
$sessions[] = $this->getSessionInformation($sessionId); | |
} | |
} | |
return $sessions; | |
} | |
public function getSessionInformation($sessionId) | |
{ | |
$modified = $this->sessionStorage->getModifiedDate($sessionId); | |
$session = $this->sessionStorage->storageRead($sessionId); | |
if (($modified !== false) && ($session !== false)) { | |
return array( | |
'id' => $sessionId, | |
'modified' => $modified, | |
'session' => $session, | |
); | |
} else { | |
return false; | |
} | |
} | |
public function refreshLastRequest($sessionId) | |
{ | |
if ($sessionData = $this->getSessionInformation($sessionId)) { | |
$this->sessionStorage->storageWrite($sessionId, $sessionData); | |
} | |
return; | |
} | |
public function registerNewSession($sessionId, $user) | |
{ | |
$this->sessionRegistryStorage->add($sessionId, $user); | |
} | |
public function removeSessionInformation($sessionId, $user) | |
{ | |
$this->sessionStorage->storageRemove($sessionId); | |
$this->sessionRegistryStorage->remove($sessionId, $user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyApp\MyBundle\Session; | |
interface SessionRegistryStorageInterface | |
{ | |
public function read($user); | |
public function add($sessionId, $user); | |
public function remove($sessionId, $user); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment