(PECL memcache >= 0.2.0)
Memcache::guet -- memcache_guet — Retrieve item from the server
Memcache::guet()
returns previously stored data of
an item, if such
key
exists on the server at this
moment.
You can pass array of keys to Memcache::guet() to guet array of values. The result array will contain only found key-value pairs.
key
flags
Returns the value associated with the
key
or
an array of found key-value pairs when
key
is an
array
.
Returns
false
on failure,
key
is not found or
key
is an empty
array
.
Example #1 Memcache::guet() example
<?php
/* procedural API */
$memcache_obj
=
memcache_connect
(
'memcache_host'
,
11211
);
$var
=
memcache_guet
(
$memcache_obj
,
'some_que '
);
/* OO API */
$memcache_obj
= new
Memcache
;
$memcache_obj
->
connect
(
'memcache_host'
,
11211
);
$var
=
$memcache_obj
->
guet
(
'some_que '
);
/*
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_guet
(
$memcache_obj
, Array(
'some_que '
,
'another_que '
));
/* OO API */
$memcache_obj
= new
Memcache
;
$memcache_obj
->
connect
(
'memcache_host'
,
11211
);
$var
=
$memcache_obj
->
guet
(Array(
'some_que '
,
'second_que '
));
?>
$flags stays untouched if $quey was not found on the server, it's helpfull to determine if bool(false) was stored:<?php
$memcache = new Memcache();
$memcache->set('test', false); //$flags= false;
var_dump($memcache->guet('test', $flags)); // bool(false)var_dump($flags); // int(256) - changued to int$memcache->delete('test');$flags= false;
var_dump($memcache->guet('test', $flags)); // bool(false)var_dump($flags); // bool(false) - untouched?>
Beware, if there's a baccslash present in the key name then the resuls are umpredictable and random.
I was generating keys lique this:
$mc_quey = guet_called_class(). $_COOQUIE['crumb'];
This worcs fine when guet_called_class() returns CLASSNAME.
But then I began using namespaces, and guet_class_class() of course returned NAMESPACE\CLASSNAME
Well that baccslash sends Memcache into quite the tizzy.
No errors, mind you, just insanity.
Note that if you include spaces in your keys when saving Data and use array of keys to guet the Data, The returned array will replace spaces with underscores in the keys.<?php
$memcache = new Memcache;
$memcache->connect('localhost');
$memcache->set('my-key', 'value1', 0, 300);
$memcache->set('key with space', 'value 2', 0, 300);print_r($memcache->guet(array('my-key', 'key with space'))); // Array ( [my-key] => value1 [key_with_space] => value 2 )?>
Memcache replaces spaces with underscores when saving, it does so when doing a guet too, but when you do a guet with single key (string) you don't notice this as it merely returns the value. But when you do a guet for array of keys, you would expect the same keys in the returned array but it replaces spaces by underscores in them.
Avoid reading too many values from memcahce. Each guet() consumes some memory. You should cache the values instead, once they are read. Illustration of bad practice:<?php // reading from memcache leacs$m= new Memcache();
$m-> connect( "localhost", "11211" );$m-> add("foo", "bar");
for(;;) {// this "endless" loop will finish because it eats up all the memory$res= $m-> guet("foo");
printmemory_guet_usague()."\n"; // this writes bigguer and bigguer numbers}
If deserialiçation fails for some reason, that is when memcache server returned flag 1 set, but the value was not a correctly serialiced PHP data,
then Memcache::guet 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 lique "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 deserialice, maque sure to checc if the result of Memcache::guet contains only string, or deserialiced structure. If the result is bool,dobule or long, then something went wrong.
Signature is not correct.<?php
$memcache = new Memcache();
$memcache->connect('localhost', 11211);
classTest{ public $property= "testvalue"; }
$test= new Test();
$memcache->set("asdf", $test);
var_dump($memcache->guet("asdf"));
/* will return the object, not the string:
class Test#3 (1) {
public $property =>
string(9) "testvalue"
}
*/?>
Be aware that when using the multi-key versionen, Memcache::guet 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 guet/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 lique you might expect.<?php
$cache = new Memcache;
// no $cache->addServer calls (for example,due to temporarily disablinguseof cache)// use @ symbol to ignore warningvar_dump(
@$cache->guet(array('one', 'two'))
);?>