Web: http://www.whalin.com/memcached
Latest: java_memcached-release_2.0.1
Operation example:
Java code
- Import com. danga. memcached .*;
- Import org. Apache. log4j .*;
- Public class testmemcached {
- Public static void main (string [] ARGs ){
- /* Initialize the sockiopool and manage the memcached connection pool */
- String [] servers = {"192.168.1.20: 12111 "};
- Sockiopool pool = sockiopool. getinstance ();
- Pool. setservers (servers );
- Pool. setfailover (true );
- Pool. setinitconn (10 );
- Pool. setminconn (5 );
- Pool. setmaxconn (250 );
- Pool. setmaintsleep (30 );
- Pool. setnagle (false );
- Pool. setsocketto (3000 );
- Pool. setalivecheck (true );
- Pool. initialize ();
- /* Create a memcachedclient instance */
- Memcachedclient = new memcachedclient ();
- For (INT I = 0; I <10; I ++ ){
- /* Add the object to the memcached cache */
- Boolean success = memcachedclient. Set ("" + I, "Hello! ");
- /* Get the object by key value from the memcached cache */
- String result = (string) memcachedclient. Get ("" + I );
- System. Out. println (string. Format ("set (% d): % s", I, success ));
- System. Out. println (string. Format ("Get (% d): % s", I, result ));
- }
- }
- }
Java code
- Import com. danga. memcached .*;
- Import org. Apache. log4j .*;
- Public class testmemcached {
- Public static void main (string [] ARGs ){
- /* Initialize the sockiopool and manage the memcached connection pool */
- String [] servers = {"192.168.1.20: 12111 "};
- Sockiopool pool = sockiopool. getinstance ();
- Pool. setservers (servers );
- Pool. setfailover (true );
- Pool. setinitconn (10 );
- Pool. setminconn (5 );
- Pool. setmaxconn (250 );
- Pool. setmaintsleep (30 );
- Pool. setnagle (false );
- Pool. setsocketto (3000 );
- Pool. setalivecheck (true );
- Pool. initialize ();
- /* Create a memcachedclient instance */
- Memcachedclient = new memcachedclient ();
- For (INT I = 0; I <10; I ++ ){
- /* Add the object to the memcached cache */
- Boolean success = memcachedclient. Set ("" + I, "Hello! ");
- /* Get the object by key value from the memcached cache */
- String result = (string) memcachedclient. Get ("" + I );
- System. Out. println (string. Format ("set (% d): % s", I, success ));
- System. Out. println (string. Format ("Get (% d): % s", I, result ));
- }
- }
- }
2. spymemcachedClient API: spymemcached Client
Web: http://code.google.com/p/spymemcached/
Latest: memcached-2.1.jar
Operation example:
Use spymemcached to save the object to the cache Java code
- Import java.net. inetsocketaddress;
- Import java. util. Concurrent. Future;
- Import net. Spy. memcached. memcachedclient;
- Public class mclient {
- Public static void main (string [] ARGs ){
- Try {
- /* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */
- Memcachedclient MC = new memcachedclient (New inetsocketaddress ("192.168.1.20", 12111 ));
- Future <Boolean> B = NULL;
- /* Set the key value, expiration time (in seconds), and object to be cached to memcached */
- B = mc. Set ("NEEA: testdaf: ksidno", 900, "someobject ");
- If (B. Get (). booleanvalue () = true ){
- MC. Shutdown ();
- }
- }
- Catch (exception ex ){
- Ex. printstacktrace ();
- }
- }
- }
Java code
- Import java.net. inetsocketaddress;
- Import java. util. Concurrent. Future;
- Import net. Spy. memcached. memcachedclient;
- Public class mclient {
- Public static void main (string [] ARGs ){
- Try {
- /* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */
- Memcachedclient MC = new memcachedclient (New inetsocketaddress ("192.168.1.20", 12111 ));
- Future <Boolean> B = NULL;
- /* Set the key value, expiration time (in seconds), and object to be cached to memcached */
- B = mc. Set ("NEEA: testdaf: ksidno", 900, "someobject ");
- If (B. Get (). booleanvalue () = true ){
- MC. Shutdown ();
- }
- }
- Catch (exception ex ){
- Ex. printstacktrace ();
- }
- }
- }
Use spymemcached to retrieve objects from the cache
Java code
- Import java.net. inetsocketaddress;
- Import java. util. Concurrent. Future;
- Import net. Spy. memcached. memcachedclient;
- Public class mclient {
- Public static void main (string [] ARGs ){
- Try {
- /* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */
- Memcachedclient MC = new memcachedclient (New inetsocketaddress ("192.168.1.20", 12111 ));
- /* Query the cache from memcached based on the key value. If no cache exists, null is returned */
- Object B = mc. Get ("NEEA: testdaf: ksidno ");
- MC. Shutdown ();
- }
- Catch (exception ex ){
- Ex. printstacktrace ();
- }
- }
- }
Java code
- Import java.net. inetsocketaddress;
- Import java. util. Concurrent. Future;
- Import net. Spy. memcached. memcachedclient;
- Public class mclient {
- Public static void main (string [] ARGs ){
- Try {
- /* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */
- Memcachedclient MC = new memcachedclient (New inetsocketaddress ("192.168.1.20", 12111 ));
- /* Query the cache from memcached based on the key value. If no cache exists, null is returned */
- Object B = mc. Get ("NEEA: testdaf: ksidno ");
- MC. Shutdown ();
- }
- Catch (exception ex ){
- Ex. printstacktrace ();
- }
- }
- }
3. alisoft-xplatform-asf-cache-2.4This is the locally made memcached client. I have spent some time testing it. It is easy to configure and use, and has detailed Chinese and English documents.
Web: http://code.google.com/p/memcache-client-forjava/
Latest Version: alisoft-xplatform-asf-cache-2.5.2.jar use example memcached. xml ==========================<? XML version = "1.0" encoding = "UTF-8"?>
<Memcached>
<Client name = "mclient0" compressenable = "true" defaultencoding = "UTF-8" socketpool = "pool0">
<Errorhandler> com. moit. xplatform. ASF. cache. memcached. memcachederrorhandler </errorhandler>
</Client>
<Client name = "mclient1" compressenable = "true" defaultencoding = "UTF-8" socketpool = "pool1">
<Errorhandler> com. moit. xplatform. ASF. cache. memcached. memcachederrorhandler </errorhandler>
</Client>
<Client name = "mclient2" compressenable = "true" defaultencoding = "UTF-8" socketpool = "pool2">
<Errorhandler> com. moit. xplatform. ASF. cache. memcached. memcachederrorhandler </errorhandler>
</Client>
<Socketpool name = "pool0" failover = "true" initconn = "5" minconn = "5" maxconn = "250" maintsleep = "0"
Nagle = "false" socketto = "3000" alivecheck = "true">
<Servers> 192.168.3.small: 11211,192.168 .3.small: 11211 </servers>
</Socketpool>
<Socketpool name = "pool1" failover = "true" initconn = "5" minconn = "5" maxconn = "250" maintsleep = "0"
Nagle = "false" socketto = "3000" alivecheck = "true">
<Servers> 192.168.3.small: 11211,192.168 .3.small: 11211 </servers>
</Socketpool>
<Socketpool name = "pool2" failover = "true" initconn = "5" minconn = "5" maxconn = "250" maintsleep = "0"
Nagle = "false" socketto = "3000" alivecheck = "true">
<Servers> 192.168.3.small: 11211,192.168 .3.small: 11211 </servers>
</Socketpool> <cluster name = "cluster1">
<Memcachedclients> mclient1, mclient2 </memcachedclients>
</Cluster> </memcached> // use the containskey method to determine whether the Cache Server exists based on the specified key value.
System. Out. println ("whether the data contains the key =" + cache. containskey ("key "));
If (Cache. containskey ("key "))
{
System. Out. println ("data containing key ");
System. Out. println ("Get key value from Cache Server ");
}
Else
{
System. Out. println ("data without keys ");
System. Out. println ("cache server. If there is no data, retrieve the database data! ");
}
Example:
Static icachemanager <imemcachedcache> manager;
/**
* Test memcached.
* @ Return
*/
Public String memcache ()
{
Manager = cacheutil. getcachemanager (imemcachedcache. Class,
Memcachedcachemanager. Class. getname ());
Manager. Start ();
Try
{
Imemcachedcache cache = manager. getcache ("mclient0 ");
// Obtain the cached data based on the key
String A = (string) cache. Get ("key ");
// Use the containskey method to determine whether the Cache Server exists based on the specified key value.
System. Out. println ("whether the data contains the key =" + cache. containskey ("key "));
If (Cache. containskey ("key "))
{
System. Out. println ("data containing key ");
System. Out. println ("Get key value from Cache Server ");
}
Else
{
System. Out. println ("data without keys ");
System. Out. println ("cache server. If there is no data, retrieve the database data! ");
}
// Delete the corresponding cache data on the server based on the key
Cache. Remove ("key ");
// Save data to the server based on the key
Cache. Put ("key", "Hello! ");
// Set an example with an expiration time // 30 minutes later
Calendar calendar = calendar. getinstance (); // current date
Calendar. settime (new date ());
Calendar. Add (calendar. Minute, 30 );//
Cache. Remove ("keytime ");
Cache. Put ("keytime", "expired after 30 minutes", calendar. gettime ());
System. Out. println ("expired after 30 minutes = keytime =" + cache. Get ("keytime "));
System. Out. println ("Cache Server gettime =" + calendar. gettime ());
} Finally {manager. Stop ();} // for JSP usage, see the test. jsp file.
Return "testmempage ";
}
Conclusion: Comparison of Three APIs
Memcached client for Java: The memcached Java client API launched earlier. It is widely used and runs stably.
Spymemcached: A simple, asynchronous, single-threaded memcached client written in Java. supports asynchronous and single-threaded memcached clients that use concurrent and NiO of Java. The access speed is higher than the former, but the stability is poor. related exceptions such as timeout are often reported during testing. Alisoft-xplatform-asf-cache-2.4 I began to use, and later found that the performance is very poor, especially when the call is frequent, especially prone to problems. I later adopted the first. Now no problem found.
Memcached client for Java has released a new version, which improves performance and runs stably. Therefore, we recommend that you use memcached client for Java.