当前位置: 首页 > redis, 分布式系统, 缓存系统 > 正文

redis java 客户端实例

1 星2 星3 星4 星5 星 (1 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share

先从https://github.com/xetorthio/jedis/downloads下载jedis-2.1.0.jar.
你的数据需要分片,就要用ShardedJedisPool,单机存储的话就用JedisPool。

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
 
public class jedis {
 
 
	static JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
	static{
		jedisPoolConfig.setMaxActive(1000);
		jedisPoolConfig.setMaxIdle(30);
		//jedisPoolConfig.setTestOnBorrow(true);
		jedisPoolConfig.setWhenExhaustedAction( GenericObjectPool.WHEN_EXHAUSTED_FAIL) ;
	}
 
	//static JedisPool pool = new JedisPool(jedisPoolConfig, "10.10.110.140");
	static JedisPool pool = new JedisPool(jedisPoolConfig, "10.10.110.140" ,6379,5000); 
	static JedisPool pool2 = new JedisPool(jedisPoolConfig, "10.10.110.140" ,6380,5000); 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
		Jedis jedis = pool.getResource(); 
		try {
 
                       testPipeLine(jedis);
                       testShardedJedisPoolPipe();
                        testLpush(jedis);
 
 
			} finally {
			  /// ... it's important to return the Jedis instance to the pool once you've finished using it
			  pool.returnResource(jedis);
			}
			/// ... when closing your application:
			pool.destroy(); 
 
 
 
	}
 
	public static void testLpush(Jedis jedis){
            jedis.lpush("list","sssssssss" );
 
			System.out.println("------"+jedis.llen("list"));
 
			System.out.println("------"+jedis.lrange("list", 0, 1));*/
 
	}
 
	public static void testShardedJedisPoolPipe(){
		long start = System.currentTimeMillis();
			List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
			JedisShardInfo jsi =  new JedisShardInfo("10.10.110.140", "6379");
			jsi.setTimeout(5);
			shards.add(jsi);
 
			Config config = new Config();
	        config.maxActive = 1;
	        config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
 
 
			ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
 
			ShardedJedisPipeline sjp = new ShardedJedisPipeline(){
				public void execute() {
					for(int i=1 ;i<=50 ;i++){
					 lrange("list"+i, 0, 19);
					}
				}
			};
 
			List<Object> results = pool.getResource().pipelined(sjp);
 
			for(int i=1 ;i<=50 ;i++){
				sjp.getrange("list"+i, 0, 19);
			}
 
			results = sjp.getResults();
 
 
			ArrayList<String> weiboIds = new ArrayList<String>();
 
			for(Object o : results){
				weiboIds.addAll((Collection<? extends String>) o);
	        }
 
        for(String o : weiboIds){
	        	System.out.println("--------"+o);
	        }
			Collections.sort(weiboIds);
 
			 System.out.println("total-----"+(System.currentTimeMillis() - start)+"===="+weiboIds.size() );
	}
 
	public static void testPipeLine(Jedis jedis){
 
 
 
 
			 long start = System.currentTimeMillis();
			List<Object> results = jedis.pipelined(
					new PipelineBlock() {
			            public void execute() {
 
			            }
					}
			);
 
 
			Pipeline p = jedis.pipelined();
 
 
			for(int i=1 ;i<=2000 ;i++){
				p.lrange("list"+i, 0, 19);
			}						
	        results = p.syncAndReturnAll();
 
	        ArrayList<String> weibos = new ArrayList<String>();  
 
	        for(Object o : results){
	        	weibos.addAll((Collection<? extends String>) o);
	        }
 
	        for(String o : weibos){
	        	System.out.println("--------"+o);
	        }
 
	        Collections.sort(weibos);
 
	        System.out.println("total-----"+(System.currentTimeMillis() - start)+"===="+weibos.size() );
 
	}
 
 
}

本文固定链接: http://www.chepoo.com/redis-java-client-demo.html | IT技术精华网

redis java 客户端实例:等您坐沙发呢!

发表评论