jedis 连接redis:
使用jedis如何操作redis,但是其实方法是跟redis的操作大部分是相对应的。
所有的redis命令都对应jedis的一个方法
1、在macen工程中引入jedis的jar包
1 2 3 4
| <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
|
2、建立测试工程
1 2 3 4 5 6 7 8 9 10 11
| public class JedisTest {
@Test public void testJedis()throws Exception{ Jedis jedis = new Jedis("192.168.241.133",6379); jedis.set("test", "my forst jedis"); String str = jedis.get("test"); System.out.println(str); jedis.close(); } }
|
3、点击运行
使用jedisPool连接redis
jedis可以连接redis,为何需要Jedispool?
每次连接需要创建一个连接、执行完后就关闭,非常浪费资源,所以使用jedispool(连接池)连接。原理和jdbc操作数据库一样。
1、添加依赖pom.xml中添加如下依赖
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency>
|
2、redis配置文件
1 2 3 4 5 6 7 8
| redis.host=121.4.60.79 redis.port=6379 redis.timeout=3 redis.password=123456 redis.poolMaxTotal=10 redis.poolMaxIdle=10 redis.poolMaxWait=3
|
注:idea会提示Cannot resolve configuration property。不影响运行。
3、绑定配置文件
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
| package com.mc.redis;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;
@Component @ConfigurationProperties(prefix="redis") public class RedisConfig { private String host; private int port; private int timeout; private String password; private int poolMaxTotal; private int poolMaxIdle; private int poolMaxWait; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getPoolMaxTotal() { return poolMaxTotal; } public void setPoolMaxTotal(int poolMaxTotal) { this.poolMaxTotal = poolMaxTotal; } public int getPoolMaxIdle() { return poolMaxIdle; } public void setPoolMaxIdle(int poolMaxIdle) { this.poolMaxIdle = poolMaxIdle; } public int getPoolMaxWait() { return poolMaxWait; } public void setPoolMaxWait(int poolMaxWait) { this.poolMaxWait = poolMaxWait; } }
|
4、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
| package com.mc.redis;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig;
@Service public class RedisPoolFactory {
@Autowired RedisConfig redisConfig; @Bean public JedisPool JedisPoolFactory() { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle()); poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal()); poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000); JedisPool jp = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getTimeout()*1000, redisConfig.getPassword(), 0); return jp; } }
|
5、测试使用
注人
1 2
| @Autowired JedisPool jedisPool;
|