赞
踩
#redis连接地址 spring.redis.host=127.0.0.1 #redis连接端口 spring.redis.port=6379 #redis使用第几个数据库 spring.redis.database=3 #redis连接超时时间 spring.redis.timeout=1800000 #redis密码 spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=20 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=5 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0
@Configuration public class RedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.database}") private int database; @Value("${spring.redis.jedis.pool.max-active}") private int maxActive; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.min-idle}") private int minIdle; @Value("${spring.redis.jedis.pool.max-wait}") private long maxWaitMillis; @Bean public JedisPool redisPoolFactory(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMinIdle(minIdle); if(password==null || "".equals(password.trim())) { password=null; } JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout,password,database); return jedisPool; } }
@SpringBootTest
class RedisDemoApplicationTests {
@Autowired
private JedisPool jedisPool;
@Test
void jedisTest() {
Jedis jedis = jedisPool.getResource();
String pong = jedis.ping();
System.out.println("连接成功:" + pong);
jedis.close();
}
}
响应结果
与Redis命令行操作一样
@Test public void keyTest(){ Jedis jedis = jedisPool.getResource(); jedis.set("k1","v1"); jedis.set("k2","v2"); jedis.set("k3","v3"); Set<String> keys = jedis.keys("*"); System.out.println(keys.size()); for (String key : keys) { System.out.println(key); } System.out.println(jedis.exists("k1")); System.out.println(jedis.ttl("k1")); System.out.println(jedis.get("k1")); jedis.close(); }
操作响应结果:
@Test
public void stringTest(){
Jedis jedis = jedisPool.getResource();
jedis.mset("str1","v1","str2","v2","str3","v3");
System.out.println(jedis.mget("str1","str2","str3"));
jedis.close();
}
操作响应结果:
@Test
public void listTest(){
Jedis jedis = jedisPool.getResource();
jedis.lpush("myList","v1","v2","v3","v4");
System.out.println(jedis.lrange("myList", 0, 2));
jedis.close();
}
操作响应结果:
@Test public void hashTest(){ Jedis jedis = jedisPool.getResource(); Map<String, String> map = new HashMap<>(); map.put("name", "张三"); map.put("sex", "男"); map.put("age", "12"); jedis.hmset("user1",map); List<String> users = jedis.hmget("user1", "name", "sex", "age"); for (String user : users) { System.out.println(user); } jedis.close(); }
操作响应结果:
@Test public void setTest(){ Jedis jedis = jedisPool.getResource(); jedis.sadd("mySet","v1","v2","v3","v4"); jedis.sadd("mySet","v2"); Set<String> mySet = jedis.smembers("mySet"); for (String value : mySet) { System.out.print(value + " "); } System.out.println(""); System.out.println("==========="); jedis.srem("mySet","v3"); Set<String> mySet1 = jedis.smembers("mySet"); for (String value : mySet1) { System.out.print(value + " "); } jedis.close(); }
操作响应结果:
ZSet
@Test
public void zsetTest(){
Jedis jedis = jedisPool.getResource();
jedis.zadd("myZSet", 100, "u1");
jedis.zadd("myZSet", 101, "u2");
jedis.zadd("myZSet", 98, "u3");
jedis.zadd("myZSet", 105, "u4");
jedis.zadd("myZSet", 91, "u5");
Set<Tuple> myZSet = jedis.zrangeWithScores("myZSet", 0, -1);
for (Tuple tuple : myZSet) {
System.out.println(tuple.getElement() + "--->" + tuple.getScore());
}
jedis.close();
}
操作响应结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。