PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/www/redis/Rediska/Zend/Session/SaveHandler/Redis.php

http://github.com/isaacsu/twich
PHP | 293 lines | 122 code | 43 blank | 128 comment | 11 complexity | 2e401126bedd233d2d0e93ae6f3d6384 MD5 | raw file
Possible License(s): GPL-2.0, JSON
  1. <?php
  2. /**
  3. * @see Rediska
  4. */
  5. require_once 'Rediska.php';
  6. /**
  7. * @see Rediska_Zend_Session_Set
  8. */
  9. require_once 'Rediska/Zend/Session/Set.php';
  10. /**
  11. * @see Zend_Session
  12. */
  13. require_once 'Zend/Session.php';
  14. /**
  15. * @see Zend_Config
  16. */
  17. require_once 'Zend/Config.php';
  18. /**
  19. * @see Zend_Session_SaveHandler_Interface
  20. */
  21. require_once 'Zend/Session/SaveHandler/Interface.php';
  22. /**
  23. * @see Zend_Session_SaveHandler_Exception
  24. */
  25. require_once 'Zend/Session/SaveHandler/Exception.php';
  26. /**
  27. * Redis save handler for Zend_Session
  28. *
  29. * @author Ivan Shumkov
  30. * @package Rediska
  31. * @version 0.4.2
  32. * @link http://rediska.geometria-lab.net
  33. * @licence http://www.opensource.org/licenses/bsd-license.php
  34. */
  35. class Rediska_Zend_Session_SaveHandler_Redis implements Zend_Session_SaveHandler_Interface
  36. {
  37. /**
  38. * Rediska instance
  39. *
  40. * @var Rediska
  41. */
  42. protected $_rediska;
  43. /**
  44. * Sessions set
  45. *
  46. * @var Rediska_Zend_Session_Set
  47. */
  48. protected $_set;
  49. /**
  50. * Configuration
  51. *
  52. * @var array
  53. */
  54. protected $_options = array(
  55. 'keyprefix' => 'PHPSESSIONS_',
  56. 'lifetime' => null,
  57. );
  58. /**
  59. * Construct save handler
  60. *
  61. * @param Zend_Config|array $options
  62. */
  63. public function __construct($options = array())
  64. {
  65. if ($options instanceof Zend_Config) {
  66. $options = $options->toArray();
  67. }
  68. // Set default lifetime
  69. $this->_options['lifetime'] = (integer)ini_get('session.gc_maxlifetime');
  70. // Get Rediska instance
  71. $defaultInstance = Rediska::getDefaultInstance();
  72. if ($defaultInstance && !isset($options['rediskaOptions'])) {
  73. $this->_rediska = $defaultInstance;
  74. } else {
  75. $this->_rediska = new Rediska($options['rediskaOptions']);
  76. unset($options['rediskaOptions']);
  77. }
  78. $this->setOptions($options);
  79. Rediska_Zend_Session_Set::setSaveHandler($this);
  80. $this->_set = new Rediska_Zend_Session_Set();
  81. }
  82. /**
  83. * Destructor
  84. *
  85. * @return void
  86. */
  87. public function __destruct()
  88. {
  89. Zend_Session::writeClose();
  90. }
  91. /**
  92. * Open Session
  93. *
  94. * @param string $save_path
  95. * @param string $name
  96. * @return boolean
  97. */
  98. public function open($save_path, $name)
  99. {
  100. return true;
  101. }
  102. /**
  103. * Close session
  104. *
  105. * @return boolean
  106. */
  107. public function close()
  108. {
  109. return true;
  110. }
  111. /**
  112. * Read session data
  113. *
  114. * @param string $id
  115. * @return string
  116. */
  117. public function read($id)
  118. {
  119. return $this->_rediska->get($this->_getKeyName($id));
  120. }
  121. /**
  122. * Write session data
  123. *
  124. * @param string $id
  125. * @param string $data
  126. * @return boolean
  127. */
  128. public function write($id, $data)
  129. {
  130. $this->_set[] = $id;
  131. $reply = $this->_rediska->set($this->_getKeyName($id), $data);
  132. if ($reply) {
  133. $this->_rediska->expire($this->_getKeyName($id), $this->_options['lifetime']);
  134. }
  135. return $reply;
  136. }
  137. /**
  138. * Destroy session
  139. *
  140. * @param string $id
  141. * @return boolean
  142. */
  143. public function destroy($id)
  144. {
  145. $this->_set->remove($id);
  146. $this->_rediska->delete($this->_getKeyName($id));
  147. return true;
  148. }
  149. /**
  150. * Garbage Collection
  151. *
  152. * @param int $maxlifetime
  153. * @return true
  154. */
  155. public function gc($maxlifetime)
  156. {
  157. $sessions = $this->_set->toArray();
  158. if (!empty($sessions)) {
  159. foreach($sessions as &$session) {
  160. $session = $this->_getKeyName($session);
  161. }
  162. // TODO: May by use TTL? Need benchmark.
  163. $lifeSession = $this->_rediska->get($sessions);
  164. foreach($sessions as $session) {
  165. if (!isset($lifeSession[$session])) {
  166. $sessionWithoutPrefix = substr($session, strlen($this->_options['keyprefix']));
  167. $this->_set->remove($sessionWithoutPrefix);
  168. }
  169. }
  170. }
  171. return true;
  172. }
  173. /**
  174. * Set options array
  175. *
  176. * @param array $options Options (see $_options description)
  177. * @return Rediska_Zend_Session_SaveHandler_Redis
  178. */
  179. public function setOptions(array $options)
  180. {
  181. foreach($options as $name => $value) {
  182. if (method_exists($this, "set$name")) {
  183. call_user_func(array($this, "set$name"), $value);
  184. } else {
  185. $this->setOption($name, $value);
  186. }
  187. }
  188. return $this;
  189. }
  190. /**
  191. * Set option
  192. *
  193. * @throws Zend_Session_SaveHandler_Exception
  194. * @param string $name Name of option
  195. * @param mixed $value Value of option
  196. * @return Rediska_Zend_Session_SaveHandler_Redis
  197. */
  198. public function setOption($name, $value)
  199. {
  200. $lowerName = strtolower($name);
  201. if (!array_key_exists($lowerName, $this->_options)) {
  202. throw new Zend_Session_SaveHandler_Exception("Unknown option '$name'");
  203. }
  204. $this->_options[$lowerName] = $value;
  205. return $this;
  206. }
  207. /**
  208. * Get option
  209. *
  210. * @param string $name Name of option
  211. * @return mixed
  212. */
  213. public function getOption($name)
  214. {
  215. $lowerName = strtolower($name);
  216. if (!array_key_exists($lowerName, $this->_options)) {
  217. throw new Zend_Session_SaveHandler_Exception("Unknown option '$name'");
  218. }
  219. return $this->_options[$lowerName];
  220. }
  221. /**
  222. * Set Rediska instance
  223. *
  224. * @param Rediska $rediska
  225. * @return Rediska_Zend_Session_SaveHandler_Redis
  226. */
  227. public function setRediska(Rediska $rediska)
  228. {
  229. $this->_rediska = $rediska;
  230. return $this;
  231. }
  232. /**
  233. * Get Rediska instance
  234. *
  235. * @return Rediska
  236. */
  237. public function getRediska()
  238. {
  239. return $this->_rediska;
  240. }
  241. /**
  242. * Add prefix to session name
  243. * @param string $id
  244. * @return string
  245. */
  246. protected function _getKeyName($id)
  247. {
  248. return $this->_options['keyprefix'] . $id;
  249. }
  250. }