上篇文章:SpringBoot之连接数据库
上篇文章介绍了如何在Springboot项目中快速的配置数据库连接的信息。本文将介绍Springboot中如何使用redis缓存以及相关的配置。
前提:redis已经在本地安装且正确部署了,并运行了redis-server,如下图所示。
如果没有安装redis,可以参照redis下载官网,根据相应的步骤,进行配置和运行。
一、添加依赖
将以下依赖放入到pom.xml中,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
二、添加配置
在application.properties添加如下配置。
#host
redis.hostname=127.0.0.1
#redis端口号 默认6379
redis.port=6379
redis.database=0
redis.pool.maxActive=600
redis.pool.maxIdle=300
redis.pool.maxWait=3000
redis.pool.testOnBorrow=true
三、代码实现redis工具类
先写一个处理redis工具类和注入redis的配置信息的类。
RedisConfig.java
该类是用来读取application.properties的Configuration类,加载redis配置信息。
@Configuration
@EnableAutoConfiguration
public class RedisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.redis.pool")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory getConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setUsePool(true);
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
return factory;
}
@Bean
public RedisTemplate<?, ?> getRedisTemplate() {
JedisConnectionFactory factory = getConnectionFactory();
RedisTemplate<?, ?> template = new StringRedisTemplate(factory);
return template;
}
}
RedisService.java
redis服务接口
public interface RedisService {
boolean set(String key, String value);
String get(String key);
boolean expire(String key, long expire);
boolean remove(String key);
}
RedisServiceImpl.java
该类实现接口来操作redis.该类比较简单,只适合插入String类型的数据,查询一样。
@Service("redisService")
public class RedisServiceImpl implements RedisService {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Override
public boolean set(final String key, final String value) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
return true;
}
});
return result;
}
@Override
public String get(final String key) {
String result = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
}
@Override
public boolean expire(final String key, long expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
@Override
public boolean remove(final String key) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.del(key.getBytes());
return true;
}
});
return result;
}
}
四、验证redis是否配置成功
写一个RedisController类,用来调用redis。
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisService redisService;
@RequestMapping(value = "/hello")
public String hello() {
return "hello";
}
@RequestMapping(method = RequestMethod.GET, value = "/save")
public String save(@RequestParam("key") String key, @RequestParam("value") String value) {
if (redisService.set(key, value)) {
return "success";
}
return "failed";
}
@RequestMapping(method = RequestMethod.GET, value = "/get")
public String get(@RequestParam("key") String key) {
return redisService.get(key);
}
}
好了,下面运行下,然后输入
运行成功。好了redis的简单使用就到这里了。