Created
January 17, 2013 15:36
-
-
Save Codeklopper/4556805 to your computer and use it in GitHub Desktop.
Simple memcached demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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