The former has found a demo on the Internet to learn the simple use of memcached.
Review today, I think: The connection pool is not a service-side management of things, the client should be the clients to manage things, how can be placed in the same class inside it? So the demo changed, the code is as follows:
Service side:
package studymemcached;import com.danga.memcached.sockiopool;public class memserver { private static SockIOPool pool; public Static void main (String[] args) { initpool ( ); } private static void destroypool () { Sockiopool.getinstance ("SP"). ShutDown (); } private static void initpool () { string[] addr = { "127.0.0.1:11211" }; integer[] weights = { 3 }; pool = Sockiopool.getinstance ("SP"); &nbSp; pool.setservers (addr); pool.setweights ( weights); pool.setinitconn (4); pool.setminconn (4); pool.setmaintsleep (30); pool.setmaxconn (; ) pool.setmaxidle (1000 * 30 * 30 * 6); pool.setmaintsleep (+); pool.setnagle (false); pool.setsocketto (; ) pool.setsocketconnectto (0); pool.initialize (); }}
Client:
Package studymemcached;import java.util.date;import com.danga.memcached.memcachedclient;public class puttocache { public static void main (String[] args) { memcachedclient client = new memcachedclient ("SP" ); client.setcompressenable (True); Client.setcompressthreshold (1000 * 1024); // Putting data into the cache Client.set ("Test2", "test2"); // put data into the cache and set the expiration time date date = new date (2000000); client.Set ("Test1", "Test1", date); client.add ("KKK", " KKKKK "); // Delete cached data // client.delete ("Test1"); // Get Cached data String str = (String) client.get ("Test1"); String str2 = (String) client.get ("Test2") ; system.out.println (str); system.out.println (STR2); system.out.println (( String) Client.get ("KKK")); }}
Run the service side first, and find that the program is over; it feels a little strange.
Then run the client, and sure enough, nothing has been stored in, nothing to take;
The log appears with this hint:attempting to get Sockio from uninitialized pool!
Thought, it may be that the connection pool initialization and immediately after the destruction;
Then change the server to the following:
Package studymemcached;import java.util.scanner;import com.danga.memcached.sockiopool;public class MemServer { private static SockIOPool pool; public static void main (String[] args) { scanner sc = new scanner (system.in); initpool (); while (!sc.nextLine (). Equals ("Exit ")) { } sc.close (); destroypool (); } private static void destroypool () { sockiopool.getinstance ("SP "). ShutDown (); &Nbsp; } private static void initpool () { String[] addr = { "127.0.0.1:11211" }; Integer[] weights = { 3 }; pool = sockiopool.getinstance ("SP"); pool.setservers (addr); pool.setweights ( weights); pool.setinitconn (4); pool.setminconn (4); pool.setmaintsleep (30); pool.setmaxconn (; ) pool.setmaxidle (1000 * 30 * 30 * 6); pool.setmaintsleep (+); pool.setnagle (false); pool.setsocketto (+); pool.setsocketconnectto (0); pool.initialize (); }}
Let the service-side card wait for input, and then run the client, so that you can get the connection from the connection pool?
...
...
Sure enough or not;
Still tip: Attempting to get Sockio from uninitialized pool!
Do these two parts of the code have to be placed in the same class file? What kind of logic is this?
Looking back at the memcached part of the company project code, but also the connection pool and the creation of the client instance logic in two class files, there is no problem;
Want to go, do a pseudo-separate, the client as a server static variable, and the thread pool initialized together, and then call the client in another class to access, this is naturally possible;
Later thought, the company's project is a Web project, is deployed in the Web container, whether the client and the thread pool objects are in the same classloader or context, so they can find each other?
But this can not be verified ah, but also to write a web version of the demo, too much effort,,,
So, write something like this: The Together class, there are two field, a client, a server is a separate class that holds the cache client and connection pool object, and when initializing the Together object, Initializes the client and pool in their respective construction methods, and then invokes the client's cache client in the Together object for access;
The code is as follows:
Together:
Package Studymemcached;public class Together {private MemC client; Private MemS server; Public Together () {client = new MemC (); Server = new MemS (); } public static void Main (string[] args) {String mistery = "xxx"; Together t = new Together (); T.client.set ("Mis", mistery); System.out.println (T.client.get ("Mis")); }}
Server side:
package studymemcached;import com.danga.memcached.sockiopool;public class mems { private sockiopool pool; public mems () { String[] addr = { "127.0.0.1:11211"  } ; integer[] weights = { 3 }; pool = sockiopool.getinstance ("SP"); pool.setservers (addr); Pool.setweights (weights); pool.setinitconn (4); pool.setminconn (4); Pool.setmaintsleep (+); pool.setmaxconn (; ) pool.setmaxidle (1000 * 30 * 30 * 6); Pool.setmaintsleep (+); pool.setnagle (false); pool.setsocketto (; ) Pool.setsocketconnectto (0); pool.initialize (); }}
Client side:
Package studymemcached;import java.util.date;import com.danga.memcached.memcachedclient;public class MemC { public MemCachedClient client;  PUBLIC MEMC () { client = new Memcachedclient ("SP"); client.setcompressenable (True); client.setcompressthreshold (64 * 1024); } public void set (String key, String value) { client.set (key, value, new Date (2000000)); } public string get (String key) { return (String) Client.get (key); }}
In this way, memcachedclient and Sockiopool are separated, only in the case of two instances in the same classloader;
In this way, it is found that the data can be accessed;
Looks like we can't find each other. A problem similar to not being in the same session environment;
I am on-line Baidu a day, also did not find someone to the client and pool initialization of separate write demo, can find the demo of both, are written together;
Maybe this pool is not a service side, or client is not a customer;
The company's project is to use the client as a single case, using the time getinstance to obtain data after the preservation and search;
Although the problem is solved, but still relatively confused;
Hope to have a understanding of the Great God help guide the guidance;
[Learn to accumulate]memcached clients and connection pools