vmware-marketplacememcached

Test Memcached

You can test your Memcached installation with a client like php-memcached-sasl, a simple Memcached client written in PHP.

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: Making this application’s network ports public is a significant security risk. You are strongly advised to only allow access to those ports from trusted networks. If, for development purposes, you need to access from outside of a trusted network, please do not allow access to those ports via a public IP address. Instead, use a secure channel such as a VPN or an SSH tunnel. Follow these instructions to remotely connect safely and reliably.

  • 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');
      $m->setSaslAuthData('user', 'password');
    

    Find out how to obtain the admistrator credentials and replace the values of user and password.

    NOTE: If your Memcached server doesn’t use SASL authentication, simply omit the call to setSaslAuthData().

    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
      $m->setSaslAuthData('user', 'password');  // 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