Skip to content

Instantly share code, notes, and snippets.

@bijayrungta
Created March 18, 2013 06:30
Show Gist options
  • Save bijayrungta/5185410 to your computer and use it in GitHub Desktop.
Save bijayrungta/5185410 to your computer and use it in GitHub Desktop.
<?php
if (class_exists('Memcache')) {
$server = 'localhost';
if (!empty($_REQUEST['server'])) {
$server = $_REQUEST['server'];
}
$memcache = new Memcache;
$isMemcacheAvailable = @$memcache->connect($server);
if ($isMemcacheAvailable) {
$aData = $memcache->get('data');
echo '<pre>';
if ($aData) {
echo '<h2>Data from Cache:</h2>';
print_r($aData);
} else {
$aData = array(
'me' => 'you',
'us' => 'them',
);
echo '<h2>Fresh Data:</h2>';
print_r($aData);
$memcache->set('data', $aData, 0, 300);
}
$aData = $memcache->get('data');
if ($aData) {
echo '<h3>Memcache seem to be working fine!</h3>';
echo '<h2>Data from Cache:</h2>';
print_r($aData);
} else {
echo '<h3>Memcache DOES NOT seem to be working!</h3>';
}
echo '</pre>';
}
}
if (!$isMemcacheAvailable) {
echo 'Memcache not available';
}
@bijayrungta
Copy link
Author

Make sure you install memcached and php5-memcache (not php5-memcached) library to check this.

There are two PHP Libraries for accessing Memcache in PHP.

  • php5-memcache
  • php5-memcached

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment