[Memcache] Testing Memcache Connection with PHP

1
2
3
4
5
<?php
$memcache_obj = memcache_connect('127.0.0.1', 11211);
$memcache_obj->add("name", "test");
echo $memcache_obj->get("name");
?>

If you encounter issues using this code, make sure of the following:

  1. Is the Memcache service running: Ensure your Memcache service is running on 127.0.0.1 at port 11211.
  2. Is the PHP Memcache extension installed and enabled: Make sure you have installed and enabled the PHP Memcache extension.

You can install the Memcache extension with the following command:

1
sudo apt-get install php-memcache

And enable the extension in your php.ini file:

1
extension=memcache.so

Restart your web server to apply changes:

1
sudo service apache2 restart

Or, if you’re using Nginx:

1
sudo service nginx restart

If you’re using memcached instead of memcache, make sure to use the correct extension and functions:

1
2
3
4
5
6
<?php
$memcached = new Memcached();
$memcached->addServer('127.0.0.1', 11211);
$memcached->set("name", "test");
echo $memcached->get("name");
?>

In this case, you need to install the php-memcached extension:

1
sudo apt-get install php-memcached