Skip to content

Instantly share code, notes, and snippets.

@mansurali901
Last active August 21, 2018 18:10
Show Gist options
  • Save mansurali901/d16fc3dae374f3270ac2f1c3f99ed794 to your computer and use it in GitHub Desktop.
Save mansurali901/d16fc3dae374f3270ac2f1c3f99ed794 to your computer and use it in GitHub Desktop.
A PHP Script to test memcache server with PHP and MySql
<?php
// This Script is writted according to my test environment
// Before testing script you must need to create database
// and some sample data into Database
// Follow below steps before running this PHP Script
// --> CREATE DATABASE mem_test;
// --> USE mem_test;
// --> GRANT ALL ON mem_test.* TO test@localhost IDENTIFIED BY 'testing123';
// --> CREATE TABLE sample_data (id int, name varchar(30));
// --> INSERT INTO sample_data VALUES (1, "some_data");
$mem = new Memcached();
$mem->addServer("127.0.0.1", 11211);
mysql_connect("localhost", "test", "testing123") or die(mysql_error());
mysql_select_db("mem_test") or die(mysql_error());
$query = "SELECT name FROM sample_data WHERE id = 1";
$querykey = "KEY" . md5($query);
$result = $mem->get($querykey);
if ($result) {
print "<p>Data was: " . $result[0] . "</p>";
print "<p>Caching success!</p><p>Retrieved data from memcached!</p>";
} else {
$result = mysql_fetch_array(mysql_query($query)) or die(mysql_error());
$mem->set($querykey, $result, 10);
print "<p>Data was: " . $result[0] . "</p>";
print "<p>Data not found in memcached.</p><p>Data retrieved from MySQL and stored in memcached for next time.</p>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment