Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple memcached demo
<?php
$start = microtime(true);
define('MEMCACHED_HOST', '127.0.0.1');
define('MEMCACHED_PORT', '11211');
// Connection creation
$memcache = new Memcache;
$cacheAvailable = $memcache->connect(MEMCACHED_HOST, MEMCACHED_PORT);
// init var
$dummyArray = null;
// If cache is available save cached data to array
if($cacheAvailable === true)
{
//get dummydata from key dummyData
$dummyArray = $memcache->get("dummyData");
}
// no cached data, generate data
if (!$dummyArray)
{
// Fill a dummyarray with useless data
$dummyArray = array();
$int = 100;
for($i = 0; $i < 10000; $i++)
{
// do some useless math, to keep php busy
$int = (($int * 10) / 10) * 2;
$int = ($int / 2) * 2;
$int = $int / 2;
$int++;
$dummyArray[] = $int;
}
// Do a foreach to change data in array
foreach ($dummyArray as $key => $value) {
for($i = 0; $i < 1000; $i++)
{
$value += $i;
}
$value -= 1000;
$dummyarray[$key] = $value;
}
// save dummyarray under key: "dummydata"
$memcache->set('dummyData', $dummyArray);
}
// Print data
echo "<pre>";
print_r($dummyArray);
echo "</pre>";
$end = microtime(true) - $start;
echo "<br /> Execution time: " . $end;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment