您好,登錄后才能下訂單哦!
怎么在springboot項目中使用Redis數據庫?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
依賴如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
配置文件如下:
spring: redis: open: true # 是否開啟redis緩存 true開啟 false關閉 database: 0 host: 47.104.208.124 port: 6378 password: lf.1228 # 密碼(默認為空) timeout: 6000 # 連接超時時長(毫秒) pool: max-active: 1000 # 連接池最大連接數(使用負值表示沒有限制) max-wait: -1 # 連接池最大阻塞等待時間(使用負值表示沒有限制) max-idle: 10 # 連接池中的最大空閑連接 min-idle: 5 # 連接池中的最小空閑連接
RedisConfig類:
@Configuration public class RedisConfig { @Autowired private RedisConnectionFactory factory; @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setConnectionFactory(factory); return redisTemplate; } @Bean public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForHash(); } @Bean public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) { return redisTemplate.opsForValue(); } @Bean public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForList(); } @Bean public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForSet(); } @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); } }
RedisUtils如下:
@Component public class RedisUtils { @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private ValueOperations<String, String> valueOperations; @Autowired private HashOperations<String, String, Object> hashOperations; @Autowired private ListOperations<String, Object> listOperations; @Autowired private SetOperations<String, Object> setOperations; @Autowired private ZSetOperations<String, Object> zSetOperations; /** 默認過期時長,單位:秒 */ public final static long DEFAULT_EXPIRE = 60 * 60 * 24; /** 不設置過期時長 */ public final static long NOT_EXPIRE = -1; private final static Gson gson = new Gson(); public void set(String key, Object value, long expire){ valueOperations.set(key, toJson(value)); if(expire != NOT_EXPIRE){ redisTemplate.expire(key, expire, TimeUnit.SECONDS); } } public void set(String key, Object value){ set(key, value, DEFAULT_EXPIRE); } public <T> T get(String key, Class<T> clazz, long expire) { String value = valueOperations.get(key); if(expire != NOT_EXPIRE){ redisTemplate.expire(key, expire, TimeUnit.SECONDS); } return value == null ? null : fromJson(value, clazz); } public <T> T get(String key, Class<T> clazz) { return get(key, clazz, NOT_EXPIRE); } public String get(String key, long expire) { String value = valueOperations.get(key); if(expire != NOT_EXPIRE){ redisTemplate.expire(key, expire, TimeUnit.SECONDS); } return value; } public String get(String key) { return get(key, NOT_EXPIRE); } public void delete(String key) { redisTemplate.delete(key); } /** * Object轉成JSON數據 */ private String toJson(Object object){ if(object instanceof Integer || object instanceof Long || object instanceof Float || object instanceof Double || object instanceof Boolean || object instanceof String){ return String.valueOf(object); } return gson.toJson(object); } /** * JSON數據,轉成Object */ private <T> T fromJson(String json, Class<T> clazz){ return gson.fromJson(json, clazz); } }
springboot如何封裝redis:
RedisTemplate
所在包: org.springframework.data.redis.core
作用:redis模板,redis事務,序列化支持,操作redis方法
JedisConnectionFactory
所在包:org.springframework.data.redis.connection.jedis
作用:redis連接工廠類,創建redis連接池等
RedisAutoConfiguration
所在包:org.springframework.boot.autoconfigure.data.redis
作用:將redis配置文件相關信息注入工廠類
RedisProperties
所在包:org.springframework.boot.autoconfigure.data.redis
作用:redis連接基礎類通過@ConfigurationProperties注解將配置信息注入屬性
看完上述內容,你們掌握怎么在springboot項目中使用Redis數據庫的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。