1 |
/*
|
2 |
* Like memcached.java, but here i'm jusing whalin's memcached java client : http://github.com/gwhalin/Memcached-Java-Client/
|
3 |
*
|
4 |
* I it doesn't like Serializable or Externalizable objects so either use simpler data or do the serializin steps
|
5 |
* yourself
|
6 |
*/
|
7 |
|
8 |
|
9 |
import java.io.IOException;
|
10 |
import java.io.PrintWriter;
|
11 |
|
12 |
import javax.servlet.ServletException;
|
13 |
import javax.servlet.http.HttpServlet;
|
14 |
import javax.servlet.http.HttpServletRequest;
|
15 |
import javax.servlet.http.HttpServletResponse;
|
16 |
|
17 |
import com.danga.MemCached.MemCachedClient;
|
18 |
import com.danga.MemCached.SockIOPool;
|
19 |
|
20 |
/**
|
21 |
* Servlet implementation class Memcached2
|
22 |
*/
|
23 |
public class Memcached2 extends HttpServlet {
|
24 |
private static final long serialVersionUID = 1L;
|
25 |
|
26 |
protected MemCachedClient memc = new MemCachedClient();
|
27 |
|
28 |
|
29 |
@Override
|
30 |
public void init() throws ServletException {
|
31 |
super.init();
|
32 |
|
33 |
final String[] servers = {"localhost:11211"};
|
34 |
|
35 |
SockIOPool pool = SockIOPool.getInstance();
|
36 |
pool.setServers(servers);
|
37 |
/*pool.setMinConn(1);
|
38 |
pool.setMaxConn(5);
|
39 |
|
40 |
pool.setNagle( false );
|
41 |
pool.setSocketTO( 3000 );
|
42 |
pool.setSocketConnectTO( 0 );*/
|
43 |
pool.initialize();//Do NOT forget to init the pool !
|
44 |
|
45 |
}
|
46 |
|
47 |
|
48 |
|
49 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
50 |
PrintWriter out = response.getWriter();
|
51 |
|
52 |
String key = "memcached2:myKey";
|
53 |
|
54 |
|
55 |
String data = (String) memc.get(key) ;
|
56 |
|
57 |
if (data == null) {
|
58 |
data = "Hej med dig";
|
59 |
|
60 |
memc.set(key, data );
|
61 |
out.write("created new");
|
62 |
} else {
|
63 |
out.write("from memcached: " + data );
|
64 |
|
65 |
}
|
66 |
}
|
67 |
|
68 |
}
|