There seems to be a bug at least in some versions concerning memcache multi gets.
When i add objects or arrays to the memcache and retrieve them with a multi get, the keys in the resulting array are really strange. I cannot access the objects in the array - even if they appear in a var_dump(). Other types are working properly.
testscript:
<?php
print '<pre>';
$memcache = new Memcache();
$memcache->addServer('localhost', '11211');
$key = 'memcacheBug123';
$var = array('testValue');
$memcache->set($key, $var);
$keys = array($key);
$data = $memcache->get($keys);
print 'key ('.$key.') exists? '.(array_key_exists($key, $data) ? 'yes' : 'no (but it should!)')."\n\n";
print '<hr />'."\n\n";
print print_r($data, 1)."\n\n";
$data[$key] = 'strange...';
print '<hr />'."\n\n";
print print_r($data, 1)."\n\n";
php version: 5.2.6-2ubuntu4.3
memcache extension: 3.0.1-1 $Revision: 1.83.2.24 $
memcached: 1.2.2-1+lenny1build0.8.10.1
Memcache::get
(PECL memcache >= 0.2.0)
Memcache::get — Retrieve item from the server
Açıklama
Memcache::get() returns previously stored data if an item with such key exists on the server at this moment.
You can pass array of keys to Memcache::get() to get array of values. The result array will contain only found key-value pairs.
Değiştirgeler
- key
-
The key or array of keys to fetch.
- flags
-
If present, flags fetched along with the values will be written to this parameter. These flags are the same as the ones given to for example Memcache::set(). The lowest byte of the int is reserved for pecl/memcache internal usage (e.g. to indicate compression and serialization status).
Dönen Değerler
Returns the string associated with the key or FALSE on failure or if such key was not found.
Örnekler
Örnek 1 Memcache::get() example
<?php
/* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
$var = memcache_get($memcache_obj, 'some_key');
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$var = $memcache_obj->get('some_key');
/*
You also can use array of keys as a parameter.
If such item wasn't found at the server, the result
array simply will not include such key.
*/
/* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
$var = memcache_get($memcache_obj, Array('some_key', 'another_key'));
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$var = $memcache_obj->get(Array('some_key', 'second_key'));
?>
Memcache::get
20-Nov-2009 02:43
16-Dec-2008 05:34
Note that you *cannot* use spaces in the key name! This is because the ASCII protocol uses space as a parameter delimiter, so you'll break things. And it won't give you sensible errors!
14-Oct-2008 03:18
Be aware that when using the multi-key version, Memcache::get returns bool false if no servers are configured for the pool (and possibly if other errors occur as well while attempting to fetch). Also, Memcache class throws annoying warnings on every get/set/delete-type calls if you have no servers added to the pool.
The following snippet var_dump's bool false, not an empty array like you might expect.
<?php
$cache = new Memcache;
// no $cache->addServer calls (for example,
due to temporarily disabling use of cache)
// use @ symbol to ignore warning
var_dump(
@$cache->get(array('one', 'two'))
);
?>
30-Jul-2008 12:07
It looks like memcache take only first 256 characters. So if you want to cache some (large) queries do md5 or similar before caching.
07-Jul-2008 02:39
If deserialization fails for some reason, that is when memcache server returned flag 1 set, but the value was not a correctly serialized PHP data,
then Memcache::get acts in a following way:
If it was called with a single key to retrieve, then a warning is raised, but since it was not actually a bug of a server, the warning says something confusing like "Memcached Server Error: null" and the function returns bool(false).
If it was called by passing an array (even with a single element in it), then the warning is not raised and the resulting array contains a value bool(false).
Since there are some buffer overrun bugs present in Memcached Server, which from time to time cause overwriting of [part of] data and therefore rendering it impossible to deserialize, make sure to check if the result of Memcache::get contains only string, or deserialized structure. If the result is bool,dobule or long, then something went wrong.
