spring-boot-starter-redis 整合
程式碼塊
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
更多支援
http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis
redis配置
# REDIS (RedisProperties)
spring.redis.database= # database name
spring.redis.host=localhost # server host
spring.redis.password= # server password
spring.redis.port=6379 # connection port
spring.redis.pool.max-idle=8 # pool settings ...
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.sentinel.master= # name of Redis server
spring.redis.sentinel.nodes= # comma-separated list of host:port pairs
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
JAVA程式碼
package com.pay.channel.business;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.stereotype.Component;
/**
* redis
* com.pay.channel.business.RedisBusiness
*
* @email h_anke@163.com
* @create 2017-03-06 13:57
**/
@Component
public class RedisBusiness {
private static final Logger logger = LoggerFactory.getLogger(RedisBusiness.class);
@Autowired
private RedisConnectionFactory redisConnectionFactory;
public void setEx(String key, String value , int expire) throws Exception{
RedisConnection connection = redisConnectionFactory.getConnection();
try {
connection.setEx(key.getBytes(), expire, value.getBytes());
}catch (Exception e){
logger.warn("redis 儲存資料出現情況,現發起一次重複連線", e);
if (connection != null)
connection.close();
connection = redisConnectionFactory.getConnection();
connection.setEx(key.getBytes(), expire, value.getBytes());
}finally {
if (connection != null)
connection.close();
}
}
public void del(String key) throws Exception{
RedisConnection connection = redisConnectionFactory.getConnection();
try {
connection.del(key.getBytes());
} finally {
if (connection != null)
connection.close();
}
}
public String get(String key) throws Exception{
RedisConnection connection = redisConnectionFactory.getConnection();
byte[] bytes = null;
try {
bytes = connection.get(key.getBytes());
}finally {
if (connection != null)
connection.close();
}
if (bytes == null) {
logger.warn("redis快取無此key,可能是超時失效了");
throw new Exception("redis 失效");
}
return new String(bytes);
}
public void set(String key,String value) throws Exception{
RedisConnection connection = redisConnectionFactory.getConnection();
try {
connection.set(key.getBytes(), value.getBytes());
}finally {
if (connection != null)
connection.close();
}
}
}
相關文章
- 整合持續整合工具
- 【springboot】學習4:整合JDBC、整合druid、整合mybatis、整合 SpringSecuritySpring BootJDBCUIMyBatisGse
- SpringBoot整合系列-整合JPASpring Boot
- MyBatis(九) 整合Spring、整合SpringMVCMyBatisSpringMVC
- SSM整合之CRUD環境搭建整合SSM
- ssm整合SSM
- SpringBoot整合系列-整合H2Spring Boot
- RestCloud iPaaS混合整合平臺,資料整合RESTCloud
- SpringBoot整合系列–整合MyBatis-plusSpring BootMyBatis
- 整合學習(一):簡述整合學習
- SSH框架整合配置所需JAR包(SSH整合)框架JAR
- 自動化整合:Pipeline整合Docker容器Docker
- ETL資料整合,RestCloud資料整合平臺RESTCloud
- Win10整合Cortana,Mac何時整合Siri?Win10Mac
- Spring AI與大模型Ollama如何整合整合?SpringAI大模型
- Vue整合UeditorVue
- SSM框架整合SSM框架
- Vuex之整合Vue
- CodePush整合
- 整合SSM框架SSM框架
- Spark整合hiveSparkHive
- Vitis AI 整合AI
- SSH框架整合框架
- OpenStack整合DockerDocker
- QTP整合SikuliQT
- 系統整合
- 整合SwiftLintSwift
- 整合學習
- SSM學習筆記3——整合 SpringMVC、整合SSMSSM筆記SpringMVC
- Mybatis整合Spring(ssm整合待續)-day04MyBatisSpringSSM
- API整合新一代平臺,iPaaS整合平臺API
- 企業如何資料整合?資料整合解決方案
- 五個模型整合模型
- Spring 整合 MyBatisSpringMyBatis
- SSM整合開發SSM
- SpringMVC整合MybatisSpringMVCMyBatis
- 資料庫整合資料庫
- Prometheus 整合 Node ExporterPrometheusExport