Created
December 10, 2016 17:59
-
-
Save mewm/7e313c159128a0f9690ea511b753d5c4 to your computer and use it in GitHub Desktop.
Json session savehandler for memcache. Handles both windows and unix driver.
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 | |
use Cache; | |
use Illuminate\Support\ServiceProvider; | |
use Session; | |
/** | |
* Class JsonSessionServiceProvider | |
*/ | |
class JsonSessionServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
Session::extend('memcached_json', function ($app) { | |
return new MemcachedJsonSessionSaveHandler(Cache::store('memcached')->getMemcached()); | |
}); | |
Session::extend('memcache_json', function ($app) { | |
return new MemcacheJsonSessionSaveHandler(Cache::store('memcache')->getMemcache()); | |
}); | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
} | |
} |
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 | |
use Memcached; | |
/** | |
* For unix | |
* Class MemcachedJsonSessionSaveHandler | |
*/ | |
class MemcachedJsonSessionSaveHandler implements \SessionHandlerInterface | |
{ | |
/** | |
* @var \Memcache | |
*/ | |
protected $cache; | |
/** | |
* Create new memcached session save handler | |
* | |
* @param Memcached $memcached | |
*/ | |
public function __construct(Memcached $memcached) | |
{ | |
$this->cache = $memcached; | |
} | |
/** | |
* @author Dennis Micky Jensen <root@mewm.org> | |
* | |
* @param string $savePath | |
* @param string $sessionName | |
* | |
* @return bool | |
*/ | |
public function open($savePath, $sessionName) | |
{ | |
return true; | |
} | |
/** | |
* @author Dennis Micky Jensen <root@mewm.org> | |
* @return bool | |
*/ | |
public function close() | |
{ | |
return true; | |
} | |
/** | |
* @author Dennis Micky Jensen <root@mewm.org> | |
* | |
* @param string $sessionId | |
* | |
* @return string | |
*/ | |
public function read($sessionId) | |
{ | |
$data = $this->cache->get($sessionId); | |
return serialize(json_decode($data, true)); | |
} | |
/** | |
* @author Dennis Micky Jensen <root@mewm.org> | |
* | |
* @param string $sessionId | |
* @param string $data | |
* | |
* @return bool | |
*/ | |
public function write($sessionId, $data) | |
{ | |
$_deserializedData = json_encode(unserialize($data)); | |
return $this->cache->set($sessionId, $_deserializedData, ini_get('session.gc_maxlifetime')); | |
} | |
/** | |
* @author Dennis Micky Jensen <root@mewm.org> | |
* | |
* @param string $sessionId | |
* | |
* @return bool | |
*/ | |
public function destroy($sessionId) | |
{ | |
return $this->cache->delete($sessionId); | |
} | |
/** | |
* @author Dennis Micky Jensen <root@mewm.org> | |
* | |
* @param int|string $lifetime | |
* | |
* @return bool | |
*/ | |
public function gc($lifetime) | |
{ | |
return true; | |
} | |
} |
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 | |
use Memcached; | |
/** | |
* For unix | |
* Class MemcachedJsonSessionSaveHandlerSuperGlobal | |
*/ | |
class MemcachedJsonSessionSaveHandlerSuperGlobal implements \SessionHandlerInterface | |
{ | |
/** | |
* @var \Memcache | |
*/ | |
protected $cache; | |
protected $lifetime; | |
/** | |
* Create new memcached session save handler | |
* | |
* @param Memcached $memcached | |
*/ | |
public function __construct(Memcached $memcached) | |
{ | |
$this->cache = $memcached; | |
} | |
/** | |
* Close session | |
* | |
* @return boolean | |
*/ | |
public function close() | |
{ | |
return true; | |
} | |
/** | |
* Destroy session | |
* | |
* @param string $id | |
* | |
* @return boolean | |
*/ | |
public function destroy($id) | |
{ | |
return $this->cache->delete($id); | |
} | |
/** | |
* Garbage collect. Memcache handles this with expiration times. | |
* | |
* @param int $maxlifetime | |
* | |
* @return boolean Always true | |
*/ | |
public function gc($maxlifetime) | |
{ | |
// let memcached handle this with expiration time | |
return true; | |
} | |
/** | |
* Open session | |
* | |
* @param string $savePath | |
* @param string $name | |
* | |
* @return boolean | |
*/ | |
public function open($savePath, $name) | |
{ | |
// Note: session save path is not used | |
$this->sessionName = $name; | |
$this->lifetime = ini_get('session.gc_maxlifetime'); | |
return true; | |
} | |
/** | |
* Read session data | |
* | |
* @param string $id | |
* | |
* @return string | |
*/ | |
public function read($id) | |
{ | |
$_SESSION = json_decode($this->cache->get($id), true); | |
if (isset($_SESSION) && !empty($_SESSION) && $_SESSION != null) { | |
return session_encode(); | |
} | |
return ''; | |
} | |
/** | |
* Write session data | |
* | |
* @param string $id | |
* @param string $data | |
* | |
* @return boolean | |
*/ | |
public function write($id, $data) | |
{ | |
// note: $data is not used as it has already been serialised by PHP, | |
// so we use $_SESSION which is an unserialised version of $data. | |
return $this->cache->set($id, json_encode($_SESSION), $this->lifetime); | |
} | |
} |
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 | |
/** | |
* For windows | |
* Class MemcacheJsonSessionSaveHandler | |
*/ | |
class MemcacheJsonSessionSaveHandler extends MemcachedJsonSessionSaveHandler | |
{ | |
/** | |
* Create new memcached session save handler | |
* | |
* @param \Memcache $memcache | |
*/ | |
public function __construct(\Memcache $memcache) | |
{ | |
$this->cache = $memcache; | |
} | |
/** | |
* @author Dennis Micky Jensen <root@mewm.org> | |
* | |
* @param string $sessionId | |
* @param string $data | |
* | |
* @return bool | |
*/ | |
public function write($sessionId, $data) | |
{ | |
$_deserializedData = json_encode(unserialize($data)); | |
return $this->cache->set($sessionId, $_deserializedData, MEMCACHE_COMPRESSED, ini_get('session.gc_maxlifetime')); | |
} | |
} |
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 | |
/** | |
* For windows | |
* Class MemcacheJsonSessionSaveHandlerSuperGlobal | |
*/ | |
class MemcacheJsonSessionSaveHandlerSuperGlobal extends MemcachedJsonSessionSaveHandlerSuperGlobal | |
{ | |
/** | |
* Create new memcached session save handler | |
* | |
* @param \Memcache $memcache | |
*/ | |
public function __construct(\Memcache $memcache) | |
{ | |
$this->cache = $memcache; | |
} | |
/** | |
* Write session data | |
* | |
* @param string $id | |
* @param string $data | |
* | |
* @return boolean | |
*/ | |
public function write($id, $data) | |
{ | |
// note: $data is not used as it has already been serialised by PHP, | |
// so we use $_SESSION which is an unserialised version of $data. | |
return $this->cache->set($id, json_encode($_SESSION), MEMCACHE_COMPRESSED, $this->lifetime); | |
} | |
} |
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 | |
session_set_save_handler(new MemcacheJsonSessionSaveHandlerSuperGlobal($memcache)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment