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