Few days back I’ve written an article on how to setup and start MemCached Server locally on you Mac OS with few simple steps.
There are three different ways to create Memcached Java Client.
- net.spy.spymemcached method
- com.whalin.Memcached–Java–Client method
- com.googlecode.xmemcached method
In this tutorial we will go over how to write simple Java Client using net.spy.spymemcached
library. If you have Maven Project in your Eclipse environment then please add below dependency to pom.xml
file.
Take a look at set(), get(), delete() operations.
<dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> <version>2.12.3</version> </dependency>
Once you update your pom.xml file then try updating your Java project. Next thing is to write simple Java program.
implementSpyMemcached.java
Library comes with very simple set()
, get()
, delete()
operations. Here is a complete example.
package crunchify.java.tutorials; import net.spy.memcached.MemcachedClient; import java.io.IOException; import java.net.InetSocketAddress; /** * @author Crunchify.com * Version: 1.0.0 * Details: Use dependency net.spy.spymemcached to retrieve, store Key Value pair from MemCached Server * */ public class CrunchifySpyMemcachedClient { public static void main(String[] args) { implementCrunchifySpyMemcached(); } // Approach-1: net.spy.spymemcached // Use dependency net.spy.spymemcached to retrieve, store Key Value pair from MemCached Server private static void implementCrunchifySpyMemcached() { // Get a memcache client operating on the specified memcached locations. MemcachedClient crunchifySpyMemCached; try { crunchifySpyMemCached = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); log("=====> Approach-1: SpyMemcached <=====\n"); log("==> Connected to Crunchify's Local Server Successfully." + " Host: localhost, Port: 11211"); // Set an object in the cache (using the default transcoder) regardless of any existing value. // The exp value is passed along to memcached exactly as given, and will be processed per the memcached protocol specification: // The actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from // current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a // client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time. crunchifySpyMemCached.set("Crunchify", 2000, "New York"); crunchifySpyMemCached.set("Google", 2000, "Mountain View"); crunchifySpyMemCached.set("PayPal", 2000, "San Jose"); crunchifySpyMemCached.set("Twitter", 2000, "San Francisco"); crunchifySpyMemCached.set("Amazon", 2000, "Seattle"); log("==> Total 5 Records added to MemCached using net.spy.spymemcached method\n"); // Get with a single key and decode using the default transcoder. log("Key: Google, Value: " + crunchifySpyMemCached.get("Google")); log("Key: PayPal, Value: " + crunchifySpyMemCached.get("PayPal")); log("Key: Twitter, Value: " + crunchifySpyMemCached.get("Twitter")); log("Key: Amazon, Value: " + crunchifySpyMemCached.get("Amazon")); log("Key: Crunchify, Value: " + crunchifySpyMemCached.get("Crunchify")); log("==> Total 5 Records Retrieved from MemCached using net.spy.spymemcached method\n"); // Delete the given key from the cache. crunchifySpyMemCached.delete("Crunchify"); log("==> Key:Crunchify deleted successfully\n"); log("Key: Crunchify, Value: " + crunchifySpyMemCached.get("Crunchify")); log("==> If no record found, it returns NULL\n"); } catch (IOException e) { // Prints this throwable and its backtrace to the standard error stream e.printStackTrace(); } } private static void log(Object object) { System.out.println(object); } }
Start MemCached Server locally
Go to Terminal window and type command memcached -d -p 11211
.
Tutorial: How to Install and Configure Memcached Process/Server on Mac OS X?
Eclipse Console Output:
Just run above program an a Java Application and you should see result like this.
2018-12-07 16:25:45.437 INFO net.spy.memcached.MemcachedConnection: Setting retryQueueSize to -1 2018-12-07 16:25:45.459 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue =====> Approach-1: SpyMemcached <===== ==> Connected to Crunchify's Local Server Sucessfully. Host: localhost, Port: 11211 ==> Total 5 Records added to MemCached using net.spy.spymemcached method Key: Google, Value: Mountain View Key: PayPal, Value: San Jose Key: Twitter, Value: San Francisco Key: Amazon, Value: Seattle Key: Crunchify, Value: New York ==> Total 5 Records Retrieved from MemCached using net.spy.spymemcached method ==> Key:Crunchify deleted successfully Key: Crunchify, Value: null ==> If no record found, it returns NULL
Let me know if you face any issue running program. In next tutorial we will go over another libraries
to set and get Memcached data.
What next?
Try Memcached Java Client Tutorial with two more libraries.