net spy memcached 使用demo

package memcached;

import java.io.IOException;
import java.net.InetSocketAddress;

import net.spy.memcached.MemcachedClient;

public class Client {

	private final int expiredSeconds = 100;

	private final InetSocketAddress server = new InetSocketAddress("127.0.0.1",
			10101);

	private MemcachedClient memcachedClient;

	public void init() {
		try {
			memcachedClient = new MemcachedClient(server);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void put(String key, Object obj) {
		memcachedClient.set(key, expiredSeconds, obj);
	}

	public void delete(String key) {
		memcachedClient.delete(key);
	}

	public Object get(String key) {
		return memcachedClient.get(key);
	}

	public static void main(String args[]) {
		Client c = new Client();
		c.init();
		long begin = System.currentTimeMillis();
		for(int i=0;i<100;i++) {
			c.put(i+"", i);
			System.out.println(c.get(i+""));
		}
		System.out.println((System.currentTimeMillis()-begin)+"ms");
	}
	
}

第一是设置过期时间,单位为秒,这里注意在put的时候,该值作为set的一个参数,但是如果该值大于30天的秒数的话,就会当成是unix时间来设置过期时间而不是offset。

第二行是本地的一个memcached的server地址和端口,调用MemcacedClient实例化一个MemcachedClient对象,下面就自定义put,get,delete常用方法就可以了

直接结果如下:
2014-08-22 09:35:01.891 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/127.0.0.1:10101, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2014-08-22 09:35:01.902 WARN net.spy.memcached.MemcachedConnection:  Could not redistribute to another node, retrying primary node for 0.
2014-08-22 09:35:01.904 WARN net.spy.memcached.MemcachedConnection:  Could not redistribute to another node, retrying primary node for 0.
0
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
92ms


发布了224 篇原创文章 · 获赞 209 · 访问量 72万+
展开阅读全文

Spymemcached incr 增长一个long型数据,报错说不是一个数值型值

02-08

版本spymemcached-2.10.1; 使用incr方法时报错。 我把一个long型数值放进memcached中: MemClient.getInstance().increment("world-record", 1); 其中MemClient.getInstance()得到我的单例工具类,increment方法实质就是incr。 但是报错: ----------------------------------------------- 2015-02-08 23:26:51.595 ERROR net.spy.memcached.protocol.ascii.MutatorOperationImpl: Error: CLIENT_ERROR cannot increment or decrement non-numeric value 2015-02-08 23:26:51.596 INFO net.spy.memcached.MemcachedConnection: Reconnection due to exception handling a memcached operation on {QA sa=localhost/127.0.0.1:11211, #Rops=1, #Wops=0, #iq=0, topRop=Cmd: incr Key: world-recordd Amount: 1, topWop=null, toWrite=0, interested=1}. This may be due to an authentication failure. OperationException: CLIENT: CLIENT_ERROR cannot increment or decrement non-numeric value at net.spy.memcached.protocol.BaseOperationImpl.handleError(BaseOperationImpl.java:169) at net.spy.memcached.protocol.ascii.OperationImpl.readFromBuffer(OperationImpl.java:151) at net.spy.memcached.MemcachedConnection.handleReads(MemcachedConnection.java:628) at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:543) at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:322) at net.spy.memcached.MemcachedConnection.run(MemcachedConnection.java:1047) 2015-02-08 23:26:51.597 WARN net.spy.memcached.MemcachedConnection: Closing, and reopening {QA sa=localhost/127.0.0.1:11211, #Rops=1, #Wops=0, #iq=1, topRop=Cmd: incr Key: world-recordd Amount: 1, topWop=null, toWrite=0, interested=1}, attempt 0. 2015-02-08 23:26:51.597 WARN net.spy.memcached.protocol.ascii.AsciiMemcachedNodeImpl: Discarding partially completed op: Cmd: incr Key: world-recordd Amount: 1 2015-02-08 23:26:51.599 WARN net.spy.memcached.MemcachedConnection: Could not redistribute to another node, retrying primary node for world-recordd. 2015-02-08 23:26:53.598 INFO net.spy.memcached.MemcachedConnection: Reconnecting {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=1, #iq=0, topRop=null, topWop=Cmd: add Key: world-recordd Flags: 0 Exp: 0 Data Length: 1, toWrite=0, interested=0} 2015-02-08 23:26:53.600 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@4edc35ba ---------------------------------------------- 请问这是什么原因? 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览