Created
August 21, 2018 21:29
-
-
Save mansurali901/22d922227ef439712fc8fe41db4d8bdf to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# 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); | |
$connection=mysqli_connect("localhost", "test", "testing123", "mem_test") 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 = mysqli_fetch_array(mysqli_query($connection, $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