PageRenderTime 76ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/php/sdk/google/appengine/ext/session/MemcacheSessionHandler.php

https://github.com/theosp/google_appengine
PHP | 181 lines | 59 code | 22 blank | 100 comment | 2 complexity | a56a728da477c87fa00cd4aaaf0d35c8 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2007 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * SessionHandler using App Engine Memcache API.
  19. *
  20. * Nb. Does not yet implement session locking, as not available until
  21. * MemcacheD is available for PHP runtime.
  22. *
  23. */
  24. namespace google\appengine\ext\session;
  25. /**
  26. * Remove direct interaction with Memcache object for ease of mocking in tests.
  27. */
  28. final class MemcacheContainer {
  29. private $memcache = null;
  30. /**
  31. * Initialises a Memcache instance
  32. */
  33. public function __construct() {
  34. $this->memcache = new \Memcache();
  35. }
  36. /**
  37. * Closes the Memcache instance.
  38. * @return bool true if successful, false otherwise
  39. */
  40. public function close() {
  41. return $this->memcache->close();
  42. }
  43. /**
  44. * Finds the value associated with input key, from Memcache.
  45. * @param string $key Input key from which to find value
  46. * @return string value associated with input key
  47. */
  48. public function get($key) {
  49. return $this->memcache->get($key, null);
  50. }
  51. /**
  52. * Inserts a key value pair, with expiry time, into Memcache.
  53. * @param string $key Input key to associate with the value
  54. * @param string $value Input value to be stored
  55. * @param int $expire Time until the pair can be garbage collected
  56. * @return bool true if successful, false otherwise
  57. */
  58. public function set($key, $value, $expire) {
  59. return $this->memcache->set($key, $value, null, $expire);
  60. }
  61. /**
  62. * Removes the key value pair, keyed with the input variable.
  63. * @param string $key Input key to remove key value pair
  64. * @return bool true if successful, false otherwise
  65. */
  66. public function delete($key) {
  67. return $this->memcache->delete($key);
  68. }
  69. }
  70. /**
  71. * A session handler interface using the GAE Memcache API.
  72. */
  73. final class MemcacheSessionHandler implements \SessionHandlerInterface {
  74. const SESSION_PREFIX = '_ah_sess_';
  75. private $expire = null;
  76. private $memcacheContainer = null;
  77. /**
  78. * Constructs the session handler instance.
  79. * @param MemcacheContainer $memcacheContainer Optional, for mocking in tests
  80. */
  81. public function __construct($memcacheContainer = null) {
  82. if(isset($memcacheContainer)) {
  83. $this->memcacheContainer = $memcacheContainer;
  84. }
  85. else {
  86. $this->memcacheContainer = new MemcacheContainer();
  87. }
  88. // Get session max lifetime to leverage Memcache expire functionality.
  89. $this->expire = ini_get("session.gc_maxlifetime");
  90. }
  91. /**
  92. * Opens the session handler.
  93. * @param string $savePath Not used
  94. * @param string $sessionName Not ued
  95. * @return bool true if successful, false otherwise
  96. */
  97. public function open($savePath, $sessionName) {
  98. return true;
  99. }
  100. /**
  101. * Closes the session handler.
  102. * @return bool true if successful, false otherwise
  103. */
  104. public function close() {
  105. return $this->memcacheContainer->close();
  106. }
  107. /**
  108. * Read an element from Memcache with the given ID.
  109. * @param string $id Session ID associated with the data to be retrieved
  110. * @return string data associated with that ID or bool false on failure
  111. */
  112. public function read($id) {
  113. // TODO: Implement locking. b/7701850
  114. return $this->memcacheContainer->get(self::SESSION_PREFIX . $id);
  115. }
  116. /**
  117. * Write an element to Memcache with the given ID and data.
  118. * @param string $id Session ID associated with the data to be stored
  119. * @param string $data Data to be stored
  120. * @return bool true if successful, false otherwise
  121. */
  122. public function write($id, $data) {
  123. // TODO: Implement locking. b/7701850
  124. return $this->memcacheContainer->set(
  125. self::SESSION_PREFIX . $id, $data, $this->expire);
  126. }
  127. /**
  128. * Destroy the data associated with a particular session ID.
  129. * @param string $id Session ID associated with the data to be destroyed
  130. * @return bool true if successful, false otherwise
  131. */
  132. public function destroy($id) {
  133. return $this->memcacheContainer->delete(
  134. self::SESSION_PREFIX . $id);
  135. }
  136. /**
  137. * Garbage collection method - always returns true as this is handled by the
  138. * Memcache expire function.
  139. * @param int $maxlifetime Not used
  140. * @return bool true if successful, false otherwise
  141. */
  142. public function gc($maxlifetime) {
  143. // Handled by "expire" in Memcache.
  144. return true;
  145. }
  146. /**
  147. * Configure the session handler to use the Memcache API.
  148. * @param MemcacheContainer $memcacheContainer Optional, for mocking in tests
  149. */
  150. public static function configure($memcacheContainer = null) {
  151. $handler = new MemcacheSessionHandler($memcacheContainer);
  152. session_set_save_handler($handler, true);
  153. /**
  154. * Set so that it is clear that Memcache is being used for session handling,
  155. * as retrieving session.save_handler just returns "user".
  156. */
  157. session_save_path("Memcache");
  158. }
  159. }