Install and use memcache.
1 memcache depends on libevent
brew install libevent
2 download memcache
#wget http://www.memcached.org/files/memcached-1.4.22.tar.gz
Then install configure; make install
3. Start:
'Sudo./memcached-p 11211-m 64-u shenyb-d using soft ps-ef | grep memcache 501 4859 1 0 am ?? 0: 00. 16./memcached-p 11211-m 64-u shenyb-d'
We can see that memcache has been started.
4. Test the connection using a java client
Java client:
Currently, there are two main types of clients:
<1>. https://github.com/gwhalin/Memcached-Java-Client/
<2>. http://code.google.com/p/spymemcached/depends on spy.jar
There is little difference between the two methods, but the set expiration time is set differently, and the efficiency has not been tested.
The first method is used here:
The test code is as follows:
Import java. util. date; import com. danga. memCached. memCachedClient; import com. danga. memCached. sockIOPool; public class MemcacheUtil {public static void testDanga () throws Exception {/* initialize the SockIOPool and manage the memcached connection pool */String [] servers = {"127.0.0.1: 11211 "}; sockIOPool = SockIOPool. getInstance (); pool. setServers (servers); pool. setFailover (true); pool. setInitConn (10); pool. setMinConn (5); pool. setMaxConn (250); // sets the sleep time of the main thread and wakes up every 30 seconds to maintain the pool size. setMaintSleep (30); // disable the socket cache pool. setNagle (false); // timeout pool after the connection is established. setSocketTO (3000); pool. setAliveCheck (true); pool. initialize ();/* Create a MemcachedClient instance */MemCachedClient memCachedClient = new MemCachedClient (); long start = System. currentTimeMillis (); for (int I = 0; I <1000; I ++) {memCachedClient. set (I + "", "hello" + I) ;}for (int I = 0; I <1000; I ++) {System. out. println (memCachedClient. get (I + "");} long end = System. currentTimeMillis (); System. out. println (end-start); memCachedClient. set ("name1", "test22", new Date (System. currentTimeMillis () + 3000); Thread. sleep (1, 2000); System. out. println (memCachedClient. get ("name1");} public static void main (String [] args) {try {testDanga ();} catch (Exception e) {e. printStackTrace ();}}}
About Setting expiration time:
client.set(key, value, new Date(expireTime));
However, there is a difference in how to set this time. For example, if it expires after 10 minutes, set date to System. currentTimeInMillis () + 10*60*1000.
Or 10*60*1000
The server is compatible with the two methods, one is how many seconds after expiration, and the other is when expiration,
However, because the time is set on the client and stored on the server, if the time difference between the two servers is great, the data expiration time may not match the time point I requested.
Finally, memcache is generally used to store infrequently changed data, such as website column information, product attribute information, and website footer.