Created
April 20, 2025 15:54
-
-
Save ChrisMoney/fd1e0eea6a34386bd7b92c3e889137a4 to your computer and use it in GitHub Desktop.
Cache database queries with PHP Memcache
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 | |
$memtest = new Memcached(); | |
$memtest->addServer("127.0.0.1", 11211); | |
$conn = mysql_connect("localhost", "tzvsqktpzm", "XWwgPDZ52R") or die(mysql_error()); | |
mysql_select_db("tzvsqktpzm") or die(mysql_error()); | |
$query = "SELECT ID FROM sample_data WHERE name = 'some_data'"; | |
$retval = mysql_query( $query, $conn ); | |
$result = mysql_fetch_array($retval, MYSQL_ASSOC); | |
$querykey = "KEY" . md5($query); | |
$memtest->set($querykey, $result); | |
$result2 = $memtest->get($querykey); | |
if ($result2) { | |
print "<p>Data was: " . $result2['ID'] . "</p>"; | |
print "<p>Caching success!</p><p>Retrieved data from memcached!</p>"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment