edit-icon download-icon

Java_ Spymemcache

Last Updated: May 04, 2018

Download the client

Download address

About the client

Client versions

Java example code

  1. Prepare the Java development environment. Log on to the Alibaba Cloud ECS server, and install the Java Development Kit (JDK) and commonly used integrated development environment (IDE) (such as Eclipse) on the server.

    Java JDK Download address

    Eclipse (Download address 1, Download address 2)

  2. The first example code is as follows. Copy the Java code in it into the Eclipse Project.

    Note: At this time, the code compilation will fail. A third-party JAR package download address is required to call the Memcache cache service. With this JAR package added, the code compilation will go through.

    OcsSample1.java example code (user name and password are required)

    1. import java.io.IOException;
    2. import java.util.concurrent.ExecutionException;
    3. import net.spy.memcached.AddrUtil;
    4. import net.spy.memcached.ConnectionFactoryBuilder;
    5. import net.spy.memcached.ConnectionFactoryBuilder.Protocol;
    6. import net.spy.memcached.MemcachedClient;
    7. import net.spy.memcached.auth.AuthDescriptor;
    8. import net.spy.memcached.auth.PlainCallbackHandler;
    9. import net.spy.memcached.internal.OperationFuture;
    10. public class OcsSample1 {
    11. public static void main(String[] args) {
    12. final String host = "xxxxxxxx.m.yyyyyyyyyy.ocs.aliyuncs.com";//The "Intranet address" on the console
    13. final String port ="11211"; //Default port 11211, no need to change
    14. final String username = "xxxxxxxxx";//The "instance ID" in the console. The username of the new version of ApsaraDB for Memcache can be left blank.
    15. final String password = "my_password";//The “password” provided in the email
    16. MemcachedClient cache = null;
    17. try {
    18. AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(username, password));
    19. cache = new MemcachedClient(
    20. new ConnectionFactoryBuilder().setProtocol(Protocol.BINARY)
    21. .setAuthDescriptor(ad)
    22. .build(),
    23. AddrUtil.getAddresses(host + ":" + port));
    24. System.out.println("ApsaraDB for Memcache Sample Code");
    25. //Store a piece of data with the “ocs” key into the ApsaraDB for Memcache to facilitate subsequent data verification and reading.
    26. String key = "ocs";
    27. String value = "Open Cache Service, from www.Aliyun.com";
    28. int expireTime = 1000; // Expiration time, unit: second; starts timing from the data write. After the expireTime elapses, the data will become expired and won't be read;
    29. OperationFuture<Boolean> future = cache.set(key, expireTime, value);
    30. future.get(); // The spymemcached set() method is asynchronous. The future.get() waits for the cache.set() operation to complete. You can also choose not to wait based on your needs.
    31. //Store several pieces of data into the ApsaraDB for Memcache and then you can see the statistics information on the ApsaraDB for Memcache console.
    32. for(int i=0;i<100;i++){
    33. key="key-"+i;
    34. value="value-"+i;
    35. //Run the set operation and store data into the cache
    36. expireTime = 1000; // Expiration time, unit: seconds
    37. future = cache.set(key, expireTime, value);
    38. future.get(); // Make sure the previous (cache.set()) operation has been completed
    39. }
    40. System.out.println("Set operation is completed.");
    41. //Run the get operation and read data from the cache. Read data with the “ocs” key.
    42. System.out.println("Get operation:"+cache.get(key));
    43. } catch (IOException e) {
    44. e.printStackTrace();
    45. } catch (InterruptedException e) {
    46. e.printStackTrace();
    47. } catch (ExecutionException e) {
    48. e.printStackTrace();
    49. }
    50. if (cache != null) {
    51. cache.shutdown();
    52. }
    53. }//eof
    54. }

    OcsSample2.java example code (user name and password are not required)

    1. import java.io.IOException;
    2. import java.util.concurrent.ExecutionException;
    3. import net.spy.memcached.AddrUtil;
    4. import net.spy.memcached.BinaryConnectionFactory;
    5. import net.spy.memcached.MemcachedClient;
    6. import net.spy.memcached.internal.OperationFuture;
    7. public class OcsSample2 {
    8. public static void main(String[] args) {
    9. final String host = "xxxxxxxx.m.yyyyyyyyyy.ocs.aliyuncs.com"; //The “intranet address” on the console
    10. final String port = "11211"; //Default port 11211, no need to change
    11. MemcachedClient cache = null;
    12. try {
    13. cache = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses(host + ":" + port));
    14. System.out.println("OCS Sample Code");
    15. //Store a piece of data with the “ocs” key into the ApsaraDB for Memcache to facilitate subsequent data verification and reading.
    16. String key = "ocs";
    17. String value = "Open Cache Service, from www.Aliyun.com";
    18. int expireTime = 1000; // Expiration time, unit: second; starts timing from the data write. After the expireTime elapses, the data will become expired and won't be read;
    19. OperationFuture<Boolean> future = cache.set(key, expireTime, value);
    20. future.get();
    21. //Store several pieces of data into the ApsaraDB for Memcache and then you can see the statistics information on the ApsaraDB for Memcache console.
    22. for (int i = 0; i < 100; i++) {
    23. key = "key-" + i;
    24. value = "value-" + i;
    25. //Run the set operation and store data into the cache
    26. expireTime = 1000; // expiration time,unit:second
    27. future = cache.set(key, expireTime, value);
    28. future.get();
    29. }
    30. System.out.println(”Set operation completed.");
    31. //Run the get operation and read data from the cache. Read data with the “ocs” key.
    32. System.out.println("Get operation:" + cache.get(key));
    33. } catch (IOException e) {
    34. e.printStackTrace();
    35. } catch (InterruptedException e) {
    36. e.printStackTrace();
    37. } catch (ExecutionException e) {
    38. e.printStackTrace();
    39. }
    40. if (cache != null) {
    41. cache.shutdown();
    42. }
    43. }//eof
    44. }
  3. Modify several items in OcsSample1.java in Eclipse according to your own instance information.

    The ApsaraDB for Memcache instance ID is unique and the Alibaba Cloud intranet address it corresponds to is also unique. All the information is displayed on the ApsaraDB for Memcache console. When establishing connections with your own ApsaraDB for Memcache instance, you must modify the corresponding information in OcsSample1.java based on the information.

  4. After the information is modified, you can run your own program. Run the main function, and you will see the following result in the console window within Eclipse (ignore the red INFO debugging information that may appear).

    1. OCS Sample Code
    2. Set operation is completed.
    3. Get operation: Open Cache Service, from www.Aliyun.com
Thank you! We've received your feedback.