google-templatesmemcached

Test Memcached

You can test your Memcached installation with a client like php-memcached-sasl, a simple Memcached client written in PHP.You can use this or any other client since the Bitnami Multi-tier solution for Memcached doesn’t require SASL authentication method by default.

NOTE: The Bitnami Memcached Stack does not include PHP. To use the example script below, you must have PHP installed on your machine. You can install PHP in your machine (refer to PHP Official Documentation for more information regarding how to install it), or you can launch a new server with PHP using the Bitnami LAMP Stack.

  • Download the php-memcached-sasl client from this link and begin by extracting the contents of the downloaded archive. The archive includes a simple script named example.php.

      $ curl -o php-memcache-sasl-master.zip 'https://codeload.github.com/hull-graveyard/php-memcache-sasl/zip/master'
      $ unzip php-memcache-sasl-master.zip
      $ cd php-memcache-sasl-master
      $ nano example.php
    

    The script begins by including the necessary PHP class and instantiating an object of the class:

      include('MemcacheSASL.php');
      $m = new MemcacheSASL;
    
  • Now, you have to connect to Memcached. In order to connect to the Memcached, you have two options:

    • (Recommended): Access Memcached through an SSH tunnel (refer to the FAQ for more information about SSH tunnels).
    • Open the port 11211 for remote access (refer to the FAQ for more information about opening ports).

IMPORTANT: We strongly discourage opening ports to allow inbound connections to the server from a different network. Making the application’s network ports public is a significant security risk. The recommended way for connecting two instances deployed in different networks is by using VPC network peering. If you must make it accessible over a public IP address, we recommend restricting access to a trusted list of source IP addresses and ports using firewall rules. To do so, follow the instructions below.

  • Once you have an active SSH tunnel or you opened the port for remote access, configure the client object by defining the Memcached server host and port, and SASL configuration. Replace these values in the script with actual values for your server:

      $m->addServer('host', 'port');
    

    Use the values below for Server host and port when:

    • Accessing through an SSH tunnel:

        $m->addServer('127.0.0.1', 'SOURCE-PORT');
      
    • Accessing opening ports:

        $m->addServer('SERVER-IP', '11211');
      

    You can now use the object’s add() and get() methods to add or remove values from the cache. In this example, the add() method stores the value ‘12345’ using the key ‘test’. The key can then be used with the get() method to retrieve the original value whenever needed.

      var_dump($m->add('test', '12345'));
      $var = $m->get('test');
    

    Here’s the complete code for the modified example.php script:

      <?php
      include('MemcacheSASL.php');
      $m = new MemcacheSASL;
      $m->addServer('host', 'port'); // replace these values
      var_dump($m->add('test', '12345'));
      $var = $m->get('test');
      print "$var";
      ?>
    
  • Save the file and run it:

      $ php example.php
    

The script will connect to your Memcached server, save the value to the key ‘test’, then retrieve and display it.

Last modification December 21, 2022