Java version of Memcache static

Source: Internet
Author: User
Tags delete cache

Import Java.util.Date;
Import Java.util.Map;

Import com.danga.MemCached.MemCachedClient;
Import Com.danga.MemCached.SockIOPool;

public class Memcachedutil {

Create a unique instance of the global
protected static Memcachedclient MCC = new Memcachedclient ();

protected static Memcachedutil memCached = new Memcachedutil ();

Set up a connection pool to the cache server
static {

Server list and its weights
String[] Servers = {Constants.OTHERURL_PROPERTIES.getValue ("Memcached_servers")};
Integer[] weights = {3};

Gets the instance object of the Socke connection pool
Sockiopool pool = sockiopool.getinstance ();

Setting Up Server information
Pool.setservers (servers);
Pool.setweights (weights);

Set the number of initial connections, minimum and maximum connections, and maximum processing time
Pool.setinitconn (5);
Pool.setminconn (5);
Pool.setmaxconn (250);
Pool.setmaxidle (1000 * 60 * 60 * 6);

Set the sleep time of the main thread
Pool.setmaintsleep (10);

Setting TCP parameters, connection timeouts, etc.
Pool.setnagle (FALSE);
Pool.setsocketto (3000);
Pool.setsocketconnectto (0);

Initializing the connection pool
Pool.initialize ();

Compression setting, data that exceeds the specified size (in K) is compressed
Mcc.setcompressenable (TRUE);
Mcc.setcompressthreshold (64 * 1024);
}

/**
* Protected construction method, not allowed to instantiate!
*
*/
Protected Memcachedutil () {

}

/**
* Get unique instances.
*
* @return
*/
public static Memcachedutil getinstance () {
return memCached;
}

/**
* Add a specified value to the cache.
*
* @param key
* @param value
* @return
*/
Public boolean Add (String key, Object value) {
Return Mcc.add (key, value);
}

Public boolean Add (String key, Object value, Date expiry) {
Return Mcc.add (key, value, expiry);
}

/**
* Replace content with key
*
* @param key
* @param value
* @return
*/
public boolean replace (String key, Object value) {
Return Mcc.replace (key, value);
}

public boolean replace (String key, Object value, Date expiry) {
Return Mcc.replace (key, value, expiry);
}

/**
* Delete cache based on key
*
* @param key
* @return
*/
public boolean remove (String key) {
return Mcc.delete (key);
}

/**
* Gets the object based on the specified keyword.
*
* @param key
* @return
*/
Public Object get (String key) {
return Mcc.get (key);
}

Public map<string, Object> getList (string[] keys) {
return Mcc.getmulti (keys);
}

public static void Main (string[] args) {
Memcachedutil cache = Memcachedutil.getinstance ();
Long Startdate=system.currenttimemillis ();
for (int i = 0; i <; i++) {
Cache.Add ("Test" +i, "China" +i);
// }
Long Enddate=system.currenttimemillis ();
for (int i = 0; i <; i++) {
System.out.println ("Cache key is test" + i + "value is"
+ cache.get ("test" + i));
}
System.out.print ("Get Value:" + cache.get ("test"));
}
}

Java version of Memcache static

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.