springboot 集成redis笔记
下载redis
下载地址:https://redis.io/download
或者直接按上面链接里的页面直接安装
下载完成之后直接解压到你想解压到的目录,我是创建了一个redis的目录
安装
然后进入到解压后的文件夹里面 cd redis-4.0.10
运行 make all命令
因为我安装的时候没有写篇日记,所以安装过程的图片不展示了
启动
进入解压文件里的src目录,里面有一个可运行文件./redis-server,运行该文件可直接启动redis服务
如上图所示即为启动成功
简单应用
进入redis的控制台,在src下运行./redis-cli即可进入redis的控制端,可查看里面存储的数据等
spring boot 端
pom.xml 依赖redis及jedis
jedis就是集成了redis的一些命令操作,封装了redis的java客户端。提供了连接池管理。一般不直接使用jedis,而是在其上在封装一层,作为业务的使用。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
redis.properties
# redis
spring.redis.host=localhost
spring.redis.port=6379
//我本地的redis没有设置密码
spring.redis.password=
spring.redis.database=1
spring.redis.maxActive=8
spring.redis.maxWait=-1
spring.redis.maxIdle=8
spring.redis.minIdle=1
spring.redis.timeout=3000
RedisConfigPropertis.java
package com.star.bootdemo.redis;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value = "classpath:redis.properties")
@ConfigurationProperties(prefix = "redis")
public class RedisConfigProperties {
//@Value : springboot 获取配置文件相应值
@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.maxActive}")
private int maxActive;
@Value("${spring.redis.maxWait}")
private int maxWait;
@Value("${spring.redis.maxIdle}")
private int maxIdle;
@Value("${spring.redis.minIdle}")
private int minIdle;
//getter setter 方法省略
}
RedisConfig.java
package com.star.bootdemo.redis;
import com.alibaba.fastjson.parser.ParserConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Autowired
private RedisConfigProperties redis;
@Bean
public RedisSerializer fastJson2JsonRedisSerializer(){
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
return new FastJson2JsonRedisSerializer<Object>(Object.class);
}
@Bean
public RedisConnectionFactory redisConnectionFactory(){
// redis的一些配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(redis.getMaxIdle());
config.setMinIdle(redis.getMinIdle());
config.setMaxWaitMillis(redis.getMaxWait());
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName(redis.getHost());
jedisConnectionFactory.setPort(redis.getPort());
jedisConnectionFactory.setPassword(redis.getPassword());
jedisConnectionFactory.setDatabase(redis.getDatabase());
jedisConnectionFactory.setTimeout(redis.getTimeout());
jedisConnectionFactory.setUsePool(true);
jedisConnectionFactory.setPoolConfig(config);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String,String> redisTemplate(RedisConnectionFactory factory,RedisSerializer fastJson2JsonRedisSerializer){
StringRedisTemplate redisTemplate = new StringRedisTemplate(factory);
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setHashValueSerializer(fastJson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
使用
这次我写了个UserService
package com.star.bootdemo.service;
import com.star.bootdemo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
@Cacheable(key = "'user:'+#id",value = "User")
public User getUser(int id) throws SQLException {
String sql = "select * from tbl_user where id = " + id;
User user = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class));
if(user != null){
return user;
}
return null;
}
@Override
@Cacheable(key = "'allUser'",value = "User")
public List<User> getUsers() throws SQLException {
String sql = "select * from tbl_user";
List<User> list = jdbcTemplate.query(sql,new Object[]{},new BeanPropertyRowMapper<>(User.class));
return list;
}
}
结果我列表显示用户后,redis库里有了
这个key好像跟java注解上的不一样,好像是注解里的key与value组合成一个key,把这个key作为本次数据在redis缓存里的key