• PHP Video Tutorials

PHP - Memcache::get() Function



Memcache::get() function can retrieve an item from the server.

Syntax

string Memcache::get( string $key [, int &$flags ] )
array Memcache::get( array $keys [, array &$flags ] )

Memcache::get() function can return previously stored data of an item if such a key exists on the server at this moment. We can pass an array of keys to Memcache::get() to get an array of values. The resulting array can contain only found key-value pairs.

Memcache::get() function can return a value associated with a key or an array of key-value pairs found when a key is an array. It can return false on failure, the key is not found, or the key is an empty array.

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");

   /* 
      We can also use array of keys as a parameter.
      If such item wasn't found at the server, the resulting 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"));
?>
Advertisements