-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter.cache.xml
More file actions
188 lines (159 loc) · 4.91 KB
/
chapter.cache.xml
File metadata and controls
188 lines (159 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: chapter.cache.xml 474 2012-10-29 11:01:51Z netkiller $ -->
<chapter id="index"><?dbhtml dir="cache" ?>
<title>Cache</title>
<section id="memcache">
<title>java memcached client</title>
<screen>
wget http://img.whalin.com/memcached/jdk6/log4j/java_memcached-release_1.5.1.tar.gz
tar zxvf java_memcached-release_1.5.1.tar.gz
cd java_memcached-release_1.5.1
cp java_memcached-release_1.5.1.jar /usr/local/memcached/api/java
</screen>
<para>export CLASSPATH="./:/usr/local/java/lib:/usr/local/java/jre/lib:/usr/local/memcached/api/java/java_memcached-release_1.5.1.jar:/usr/local/memcached/api/java/log4j-1.2.14.jar"</para>
<example>
<title>memcached.java</title>
<screen>
<![CDATA[
import com.danga.MemCached.*;
import org.apache.log4j.*;
public class memcached {
public static void main(String[] args) {
try{
BasicConfigurator.configure();
String[] serverlist = { "127.0.0.1:11211" };
// initialize the pool for memcache servers
SockIOPool pool = SockIOPool.getInstance( "test" );
pool.setServers( serverlist );
pool.setInitConn( 10 );
pool.setMinConn( 5 );
pool.setMaxConn( 250 );
pool.setMaintSleep( 30 );
pool.setNagle( false );
pool.setSocketTO( 3000 );
pool.initialize();
MemCachedClient mc = new MemCachedClient();
// compression is enabled by default
mc.setCompressEnable(true);
// set compression threshhold to 4 KB (default: 15 KB)
mc.setCompressThreshold(4096);
// turn on storing primitive types as a string representation
// Should not do this in most cases.
mc.setPrimitiveAsString(true);
mc.setPoolName( "test" );
for ( int i = 0; i < 10; i++ ) {
boolean success = mc.set( "" + i, "Hello!" );
String result = (String)mc.get( "" + i );
System.out.println( String.format( "set( %d ): %s", i, success ) );
System.out.println( String.format( "get( %d ): %s", i, result ) );
}
System.out.println( "\n\t -- sleeping --\n" );
try { Thread.sleep( 10000 ); } catch ( Exception ex ) { }
for ( int i = 0; i < 10; i++ ) {
boolean success = mc.set( "" + i, "Hello!" );
String result = (String)mc.get( "" + i );
System.out.println( String.format( "set( %d ): %s", i, success ) );
System.out.println( String.format( "get( %d ): %s", i, result ) );
}
}
catch (Exception e)
{
System.out.println("[Exception] - " + e.toString());
}
finally {}
}
}
]]>
</screen>
<para>test memcache</para>
<screen>
javac memcached.java
java memcached
</screen>
</example>
</section>
<section id="jedis">
<title>Jedis</title>
<para></para>
<programlisting>
<![CDATA[
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
]]>
</programlisting>
<programlisting>
<![CDATA[
package cn.netkiller.redis;
import redis.clients.jedis.Jedis;
public class JedisTest {
private static Jedis jedis;
public static void main(String[] args) {
jedis = new Jedis("192.168.2.1");
System.out.println("Server is running: "+jedis.ping());
jedis.set("foo", "bar");
String value = jedis.get("foo");
System.out.println(value);
}
}
]]>
</programlisting>
<section>
<title>认证</title>
<programlisting>
<![CDATA[
jedis = new Jedis("localhost", 6379, 15000);
jedis.auth("foobared");
]]>
</programlisting>
<screen>
</screen>
</section>
<section>
<title>jedis.keys</title>
<programlisting>
<![CDATA[
public Map<String, String> getCaptcha() {
Map<String, String> captchas = new HashMap<String, String>();
this.jedis = new Jedis(this.config.getProperty("captcha.redis"));
System.out.printf("%s: Redis server is running %s\n", this.getClass().getName(), this.jedis.ping());
Set<String> keys = jedis.keys("captcha::phone::*");
if (!keys.isEmpty()) {
for(String key:keys){
if (this.jedis.exists(key)) {
captchas.put(key.replace("captcha::phone::", ""), this.jedis.get(key)+" ["+ String.valueOf(this.jedis.ttl(key)) + "s]");
}
}
}
this.jedis.close();
return captchas;
}
]]>
</programlisting>
<programlisting>
<![CDATA[
import redis.clients.jedis.Jedis;
public class RedisKeys {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
List<String> list = jedis.keys("*");
for(int i=0; i<list.size(); i++) {
System.out.println("List of stored keys:: "+list.get(i));
}
}
}
]]>
</programlisting>
<screen>
</screen>
</section>
</section>
<section id="ehcache">
<title>Ehcache</title>
</section>
</chapter>