Skip to content

Commit ff493d2

Browse files
authored
Add peek support for memcache::get (#108)
* Add peek support for memcache::get * Add peek support for memcached::get
1 parent b23a2af commit ff493d2

File tree

7 files changed

+1317
-511
lines changed

7 files changed

+1317
-511
lines changed

src/Api/Memcache/Memcache.php

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,52 +173,56 @@ public function flush() {
173173
return true;
174174
}
175175

176-
private function getMulti($keys, $flags = null) {
176+
private function getMulti($keys, $flags = null, $for_peek = false) {
177177
$request = new MemcacheGetRequest();
178178
$response = new MemcacheGetResponse();
179179

180+
if ($for_peek) {
181+
$request->setForPeek(true);
182+
}
183+
180184
foreach ($keys as $key) {
181185
$request->addKey($key);
182186
}
183187

184-
ApiProxy::makeSyncCall('memcache', 'Get', $request, $response);
188+
try {
189+
ApiProxy::makeSyncCall('memcache', 'Get', $request, $response);
190+
} catch (Error $e) {
191+
return false;
192+
}
185193

186194
$return_value = array();
187195
foreach ($response->getItemList() as $item) {
188196
try {
189-
$return_value[$item->getKey()] = MemcacheUtils::deserializeValue(
190-
$item->getValue(), $item->getFlags());
197+
$value = MemcacheUtils::deserializeValue($item->getValue(), $item->getFlags());
198+
if ($for_peek) {
199+
$memcacheItemWithTimestamps = new MemcacheItemWithTimestamps(
200+
$value,
201+
$item->getTimestamps()->getExpirationTimeSec(),
202+
$item->getTimestamps()->getLastAccessTimeSec(),
203+
$item->getTimestamps()->getDeleteLockTimeSec());
204+
$return_value[$item->getKey()] = $memcacheItemWithTimestamps;
205+
} else {
206+
$return_value[$item->getKey()] = $value;
207+
}
191208
} catch (\UnexpectedValueException $e) {
192209
// Skip entries that cannot be deserialized.
193210
}
194211
}
195212
return $return_value;
196213
}
197214

198-
/**
199-
* Fetches previously stored data from the cache.
200-
*
201-
* @param string|string[] $keys The key associated with the value to fetch, or
202-
* an array of keys if fetching multiple values.
203-
*
204-
* @param int $flags This parameter is present only for compatibility and is
205-
* ignored. It should return the stored flag value.
206-
*
207-
* @return mixed On success, the string associated with the key, or an array
208-
* of key-value pairs when $keys is an array. On failure, false
209-
* is returned.
210-
*/
211-
public function get($keys, $flags = null) {
215+
private function getInternal($keys, $flags, $for_peek) {
212216
if (is_array($keys)) {
213-
$return_value = $this->getMulti($keys, $flags);
217+
$return_value = $this->getMulti($keys, $flags, $for_peek);
214218
if (empty($return_value)) {
215219
return false;
216220
} else {
217221
return $return_value;
218222
}
219223
} else {
220224
try {
221-
$return_value = $this->getMulti(array($keys), array($flags));
225+
$return_value = $this->getMulti(array($keys), array($flags), $for_peek);
222226
} catch (Error $e) {
223227
return false;
224228
}
@@ -230,6 +234,23 @@ public function get($keys, $flags = null) {
230234
}
231235
}
232236

237+
/**
238+
* Fetches previously stored data from the cache.
239+
*
240+
* @param string|string[] $keys The key associated with the value to fetch, or
241+
* an array of keys if fetching multiple values.
242+
*
243+
* @param int $flags This parameter is present only for compatibility and is
244+
* ignored. It should return the stored flag value.
245+
*
246+
* @return mixed On success, the string associated with the key, or an array
247+
* of key-value pairs when $keys is an array. On failure, false
248+
* is returned.
249+
*/
250+
public function get($keys, $flags = null) {
251+
return $this->getInternal($keys, $flags, false /* $for_peek */);
252+
}
253+
233254
// Not implemented:
234255
// getExtendedStats
235256
// getServerStatus
@@ -296,6 +317,23 @@ public function pconnect($host, $port = null, $timeout = 1) {
296317
return true;
297318
}
298319

320+
/**
321+
* Gets an item from memcache along with timestamp metadata.
322+
*
323+
* @param string|string[] $keys The key associated with the value to fetch, or
324+
* an array of keys if fetching multiple values.
325+
*
326+
* @param int $flags This parameter is present only for compatibility and is
327+
* ignored. It should return the stored flag value.
328+
*
329+
* @return mixed On success, the MemcacheItemWithTimestamps associated with
330+
* the key, or an array of key-MemcacheItemWithTimestamp pairs
331+
* when $keys is an array. On failure, false is returned.
332+
*/
333+
public function peek($keys, $flags = null) {
334+
return $this->getInternal($keys, $flags, true /* $for_peek */);
335+
}
336+
299337
/**
300338
* Replaces an existing item in the cache. Will fail if the key is not already
301339
* present in the cache.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* A memcache item with a value and timestamps.
4+
*/
5+
6+
namespace Google\AppEngine\Api\Memcache;
7+
8+
class MemcacheItemWithTimestamps {
9+
10+
private $value = null;
11+
private $expiration_time_sec = null;
12+
private $last_access_time_sec = null;
13+
private $delete_lock_time_sec = null;
14+
15+
/**
16+
* Constructs an instance of MemcacheItemWithTimestamps.
17+
* @param mixed $value The value of the item.
18+
* @param int $expirationTimeSec The absolute expiration time of the item.
19+
* @param int $lastAccessTimeSec The absolute last access time of the item.
20+
* @param int $deleteLockTimeSec The absolute delete lock time of the item.
21+
*/
22+
public function __construct($value,
23+
$expirationTimeSec,
24+
$lastAccessTimeSec,
25+
$deleteLockTimeSec) {
26+
$this->value = $value;
27+
$this->expiration_time_sec = $expirationTimeSec;
28+
$this->last_access_time_sec = $lastAccessTimeSec;
29+
$this->delete_lock_time_sec = $deleteLockTimeSec;
30+
}
31+
32+
/**
33+
* @return mixed The value of the item is returned.
34+
*/
35+
public function getValue() {
36+
return $this->value;
37+
}
38+
39+
/**
40+
* @return int Absolute expiration timestamp of the item in unix epoch seconds.
41+
* Returns 0 if this item has no expiration timestamp.
42+
*/
43+
public function getExpirationTimeSec() {
44+
return $this->expiration_time_sec;
45+
}
46+
47+
/**
48+
* @return int Absolute last accessed timestamp of the item in unix epoch
49+
* seconds.
50+
*/
51+
public function getLastAccessTimeSec() {
52+
return $this->last_access_time_sec;
53+
}
54+
55+
/**
56+
* @return int Absolute delete_time timestamp of the item in unix epoch
57+
* seconds. Returns 0 if this item has no expiration timestamp.
58+
*/
59+
public function getDeleteLockTimeSec() {
60+
return $this->delete_lock_time_sec;
61+
}
62+
}

src/Api/Memcache/MemcacheOptIn.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,29 @@ function memcache_pconnect($host, $port = null, $timeout = 1) {
219219
}
220220
}
221221

222+
/**
223+
* Gets an item from memcache along with timestamp metadata.
224+
*
225+
* @param Memcache $memcache_obj The cache instance to get the item from.
226+
*
227+
* @param string|string[] $keys The key associated with the value to fetch, or
228+
* an array of keys if fetching multiple values.
229+
*
230+
* @param int $flags This parameter is present only for compatibility and is
231+
* ignored. It should return the stored flag value.
232+
*
233+
* @return mixed On success, the MemcacheItemWithTimestamps associated with the
234+
* key, or an array of key-ItemWithTimestamp pairs when $keys is
235+
* an array. On failure, false is returned.
236+
*/
237+
function memcache_peek(
238+
Google\AppEngine\Api\Memcache\Memcache $memcache_obj,
239+
$keys,
240+
$flags = null
241+
) {
242+
return $memcache_obj->peek($keys, $flags);
243+
}
244+
222245
/**
223246
* Replaces an existing item in the cache. Will fail if the key is not already
224247
* present in the cache.

0 commit comments

Comments
 (0)