Skip to content

Instantly share code, notes, and snippets.

@Codeklopper
Created January 17, 2013 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Codeklopper/4556805 to your computer and use it in GitHub Desktop.
Save Codeklopper/4556805 to your computer and use it in GitHub Desktop.
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