springboot中使用redis的方法代碼詳解
特別說明:
本文針對的是新版 spring boot 2.1.3,其 spring data 依賴為 spring-boot-starter-data-redis,且其默認連接池為 lettuce
redis 作為一個高性能的內存數(shù)據(jù)庫,如果不會用就太落伍了,之前在 node.js 中用過 redis,本篇記錄如何將 redis 集成到 spring boot 中。提供 redis 操作類,和注解使用 redis 兩種方式。主要內容如下:
•docker 安裝 redis
•springboot 集成 redis
•編寫 redis 操作類
•通過注解使用 redis
安裝 redis
通過 docker 安裝,docker compose 編排文件如下:
# docker-compose.yml version: "2" services: redis: container_name: redis image: redis:3.2.10 ports: - "6379:6379"
然后在docker-compose.yml所在目錄使用docker-compose up -d命令,啟動 redis。
集成 springboot
說明:springboot 版本為 2.1.3
添加 maven 依賴
只需添加spring-boot-starter-data-redis依賴即可,并排除 lettuce 依賴,然后引入 jedis 和 jedis 的依賴 commons-pool2
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
編寫 springboot 配置文件
配置文件如下:
server: port: 8081 servlet: context-path: /sso spring: application: name: SSO cache: type: redis redis: database: 0 host: 192.168.226.5 port: 6379 # 有密碼填密碼,沒有密碼不填 password: # 連接超時時間(ms) timeout: 1000ms # 高版本springboot中使用jedis或者lettuce jedis: pool: # 連接池最大連接數(shù)(負值表示無限制) max-active: 8 # 連接池最大阻塞等待時間(負值無限制) max-wait: 5000ms # 最大空閑鏈接數(shù) max-idle: 8 # 最小空閑鏈接數(shù) min-idle: 0
編寫配置類
配置類代碼如下:
@EnableCaching//開啟緩存
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
/**
* 設置緩存管理器,這里可以配置默認過期時間等
*
* @param connectionFactory 連接池
* @return
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(60));
//注意:請勿使用先new 配置對象,然后在調用entryTtl方法的方式來操作
//會導致配置不生效,原因是調用.entryTtl方法會返回一個新的配置對象,而不是在原來的配置對象上修改
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
RedisCacheManager manager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
return manager;
}
@SuppressWarnings("all")
@Bean
public RedisTemplate<String, String> redisTemplate(JedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisSerializer stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(stringSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
//使用jedis連接池建立jedis連接工廠
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
logger.info("jedisConnectionFactory:初始化了");
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
config.setMaxWaitMillis(maxWaitMillis);
config.setMaxTotal(maxActive);
//鏈接耗盡時是否阻塞,默認true
config.setBlockWhenExhausted(true);
//是否啟用pool的jmx管理功能,默認true
config.setJmxEnabled(true);
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setPoolConfig(config);
factory.setHostName(host);
factory.setPort(port);
factory.setPassword(password);
factory.setDatabase(database);
factory.setTimeout(timeout);
return factory;
}
}
使用方法
有兩種方法來進行緩存操作,一種是在方法上添加緩存注解實現(xiàn)各種操作,一種是手動控制。個人比較喜歡手動控制,覺得這樣都在自己的掌控中。
通過注解使用
主要有以下 5 個注解:
•@CacheConfig: 類級別緩存,設置緩存 key 前綴之類的
•@Cacheable: 觸發(fā)緩存入口
•@CacheEvict: 觸發(fā)移除緩存
•@CachePut: 更新緩存
•@Caching: 組合緩存
@CacheConfig
該注解可以將緩存分類,它是類級別注解,主要用于給某個類的緩存全局配置,例子如下:
@CacheConfig(cacheNames = "redis_test")
@Service
public class RedisService {
//....
}
上面 CacheConfig 會給類下通過注解生成的 key 加上 redis_test 的前綴。
@Cacheable
方法級別注解,根據(jù) key 查詢緩存:
•如果 key 不存在,將方法返回值緩存到 redis 中
•如果 key 存在,直接從緩存中取值
例子如下:
/**
* 緩存時間,首次查詢后會緩存結果,key中的值可使用表達式計算.
* 如不提供key,將使用默認key構造方法生成一個key
* @return long
*/
@Cacheable(key = "'currentTime'")
public long getTime() {
return System.currentTimeMillis();
}
多次調用此段代碼會發(fā)現(xiàn)每次返回的值都是一樣的。
CachePut
用于更新緩存,每次調用都會想 db 請求,緩存數(shù)據(jù)
•如果 key 存在,更新內容
•如果 key 不存在,插入內容
代碼如下:
/**
* 一般用于更新查插入操作,每次都會請求db
*/
@CachePut(key = "'currentTime'+#id")
public long updateTime(String id) {
return System.currentTimeMillis();
}
每次調用此方法都會根據(jù) key 刷新 redis 中的緩存數(shù)據(jù)。
@CacheEvict
根據(jù) key 刪除緩存中的數(shù)據(jù)。allEntries=true 表示刪除緩存中所有數(shù)據(jù)。
代碼如下:
@CacheEvict(key = "'currentTime'+#id",allEntries=false)
public void deleteTime(String id) {
}
@Caching
本注解可將其他注解組合起來使用。比如下面的例子:
//value屬性為key指定前綴
@Caching(put = {@CachePut(value = "user", key = "'name_'+#user.name"),
@CachePut(value = "user", key = "'pass_'+#user.password")})
public User testCaching(User user) {
return user;
}
上面的代碼執(zhí)行后將在 redis 中插入兩條記錄。使用keys *將看到如下結果:

手動控制
手動控制就相當于 mybatis 的手寫 sql 語句,需要調用redisTemplate中的各種方法來進行緩存查詢,緩存更新,緩存刪除等操作。
使用方法參見 util/RedisUtil 中的方法。redisTemplate基本可以實現(xiàn)所有的 redis 操作。
總結
以上所述是小編給大家介紹的springboot中使用redis的方法代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
- SpringBoot項目中使用redis緩存的方法步驟
- 詳解springboot中redis的使用和分布式session共享問題
- SpringBoot+Mybatis項目使用Redis做Mybatis的二級緩存的方法
- SpringBoot使用Redisson實現(xiàn)分布式鎖(秒殺系統(tǒng))
- SpringBoot中Shiro緩存使用Redis、Ehcache的方法
- Redis在springboot中的使用教程
- Springboot使用redis進行api防刷限流過程詳解
- SpringBoot使用Redis緩存的實現(xiàn)方法
- springboot中使用redis由淺入深解析
- SpringBoot中使用Redis的完整實例
相關文章
Java網(wǎng)絡通信中ServerSocket的設計優(yōu)化方案
今天小編就為大家分享一篇關于Java網(wǎng)絡通信中ServerSocket的設計優(yōu)化方案,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04
一文快速了解spring?boot中的@idempotent注解
idempotence注解是RESTful API設計中一個重要的概念,它可以保證操作的可靠性和一致性,下面這篇文章主要給大家介紹了關于spring?boot中@idempotent注解的相關資料,需要的朋友可以參考下2024-01-01

