App Engine Python SDK version 1.8.1
[gae.git] / python / php / sdk / google / appengine / ext / session / MemcacheSessionHandler.php
blobab84f2d01847dc986d3cb77d3e21d5f7cd3270ae
1 <?php
2 /**
3 * Copyright 2007 Google Inc.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /**
18 * SessionHandler using App Engine Memcache API.
20 * Nb. Does not yet implement session locking, as not available until
21 * MemcacheD is available for PHP runtime.
25 namespace google\appengine\ext\session;
27 require_once 'google/appengine/runtime/Memcache.php';
29 /**
30 * Remove direct interaction with Memcache object for ease of mocking in tests.
32 final class MemcacheContainer {
34 private $memcache = null;
36 /**
37 * Initialises a Memcache instance
39 public function __construct() {
40 $this->memcache = new \Memcache();
43 /**
44 * Closes the Memcache instance.
45 * @return bool true if successful, false otherwise
47 public function close() {
48 return $this->memcache->close();
51 /**
52 * Finds the value associated with input key, from Memcache.
53 * @param string $key Input key from which to find value
54 * @return string value associated with input key
56 public function get($key) {
57 return $this->memcache->get($key, null);
60 /**
61 * Inserts a key value pair, with expiry time, into Memcache.
62 * @param string $key Input key to associate with the value
63 * @param string $value Input value to be stored
64 * @param int $expire Time until the pair can be garbage collected
65 * @return bool true if successful, false otherwise
67 public function set($key, $value, $expire) {
68 return $this->memcache->set($key, $value, null, $expire);
71 /**
72 * Removes the key value pair, keyed with the input variable.
73 * @param string $key Input key to remove key value pair
74 * @return bool true if successful, false otherwise
76 public function delete($key) {
77 return $this->memcache->delete($key);
81 /**
82 * A session handler interface using the GAE Memcache API.
84 final class MemcacheSessionHandler implements \SessionHandlerInterface {
86 const SESSION_PREFIX = 'sess_';
88 private $expire = null;
89 private $memcacheContainer = null;
91 /**
92 * Constructs the session handler instance.
93 * @param MemcacheContainer $memcacheContainer Optional, for mocking in tests
95 public function __construct($memcacheContainer = null) {
96 if(isset($memcacheContainer)) {
97 $this->memcacheContainer = $memcacheContainer;
99 else {
100 $this->memcacheContainer = new MemcacheContainer();
103 // Get session max lifetime to leverage Memcache expire functionality.
104 $this->expire = ini_get("session.gc_maxlifetime");
108 * Opens the session handler.
109 * @param string $savePath Not used
110 * @param string $sessionName Not ued
111 * @return bool true if successful, false otherwise
113 public function open($savePath, $sessionName) {
114 return true;
118 * Closes the session handler.
119 * @return bool true if successful, false otherwise
121 public function close() {
122 return $this->memcacheContainer->close();
126 * Read an element from Memcache with the given ID.
127 * @param string $id Session ID associated with the data to be retrieved
128 * @return string data associated with that ID or bool false on failure
130 public function read($id) {
131 // TODO: Implement locking. b/7701850
132 return $this->memcacheContainer->get(self::SESSION_PREFIX . $id);
136 * Write an element to Memcache with the given ID and data.
137 * @param string $id Session ID associated with the data to be stored
138 * @param string $data Data to be stored
139 * @return bool true if successful, false otherwise
141 public function write($id, $data) {
142 // TODO: Implement locking. b/7701850
143 return $this->memcacheContainer->set(
144 self::SESSION_PREFIX . $id, $data, $this->expire);
148 * Destroy the data associated with a particular session ID.
149 * @param string $id Session ID associated with the data to be destroyed
150 * @return bool true if successful, false otherwise
152 public function destroy($id) {
153 return $this->memcacheContainer->delete(
154 self::SESSION_PREFIX . $id);
158 * Garbage collection method - always returns true as this is handled by the
159 * Memcache expire function.
160 * @param int $maxlifetime Not used
161 * @return bool true if successful, false otherwise
163 public function gc($maxlifetime) {
164 // Handled by "expire" in Memcache.
165 return true;
170 * Configure the session handler to use the Memcache API.
171 * @param MemcacheContainer $memcacheContainer Optional, for mocking in tests
173 function configureMemcacheSessionHandler($memcacheContainer = null) {
174 $handler = new MemcacheSessionHandler($memcacheContainer);
176 session_set_save_handler($handler, true);
179 * Set so that it is clear that Memcache is being used for session handling,
180 * as retrieving session.save_handler just returns "user".
182 session_save_path("Memcache");