欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Jedis

时间:2023-08-03

Redis给Java语言提供了客户端API,称之为Jedis。Jedis API和Redis命令几乎是一样的。​ Jedis API特别简单,基本上都是创建对象调用方法即可。

(1)单机版

public void testStandalone(){ Jedis jedis = new Jedis("192.168.1.129",6379); jedis.set("name" ,"standalone"); String value = jedis.get("name"); System.out.println(value);}

(2)带有连接池

public void testJedisPool(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(20); jedisPoolConfig.setMaxIdle(5); jedisPoolConfig.setMinIdle(3); JedisPool jedisPool = new JedisPool(jedisPoolConfig , "192.168.1.129",6379 ); Jedis jedis = jedisPool.getResource(); jedis.set("name" , "pool"); String poolvalue = jedis.get("name"); System.out.println(poolvalue);}

(3)集群

public void testCluster(){ Set set = new HashSet(); set.add(new HostAndPort("192.168.1.129",7001)); set.add(new HostAndPort("192.168.1.129",7002)); set.add(new HostAndPort("192.168.1.129",7003)); set.add(new HostAndPort("192.168.1.129",7004)); set.add(new HostAndPort("192.168.1.129",7005)); set.add(new HostAndPort("192.168.1.129",7006)); JedisCluster jedisCluster = new JedisCluster(set); jedisCluster.set("name" , "colony"); String value = jedisCluster.get("name"); System.out.println(value);}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。