(資料圖片僅供參考)
我們以IDEA + SpringBoot作為 Java中整合Redis的使用 的測試環境
首先,我們需要導入Redis的maven依賴
org.springframework.boot spring-boot-starter-data-redis
其次,我們需要在配置文件中配置你的Redis配置信息,我使用的是 .yml文件格式
# redis配置 spring: redis: # r服務器地址 host: 127.0.0.1 # 服務器端口 port: 6379 # 數據庫索引(默認0) database: 0 # 連接超時時間(毫秒) timeout: 10s jedis: pool: # 連接池中的最大空閑連接數 max-idle: 8 # 連接池中的最小空閑連接數 min-idle: 0 # 連接池最大連接數(使用負值表示沒有限制) max-active: 8 # 連接池最大阻塞等待時間(使用負值表示沒有限制) max-wait: -1
對 redis 做自定義配置
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfigurer extends CachingConfigurerSupport { /** * redisTemplate 序列化使用的jdkSerializeable, 存儲二進制字節碼, 所以自定義序列化類 */ @Bean public RedisTemplateredisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { // 配置redisTemplate RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); // 設置序列化 Jackson2JsonRedisSerializer
然后,我們需要創建一個RedisUtil來對Redis數據庫進行操作
package com.zyxx.test.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; /** * @ClassName RedisUtil * @Author * @Date 2019-08-03 17:29:29 * @Version 1.0 **/ @Component public class RedisUtil { @Autowired private RedisTemplatetemplate; /** * 讀取數據 * * @param key * @return */ public String get(final String key) { return template.opsForValue().get(key); } /** * 寫入數據 */ public boolean set(final String key, String value) { boolean res = false; try { template.opsForValue().set(key, value); res = true; } catch (Exception e) { e.printStackTrace(); } return res; } /** * 根據key更新數據 */ public boolean update(final String key, String value) { boolean res = false; try { template.opsForValue().getAndSet(key, value); res = true; } catch (Exception e) { e.printStackTrace(); } return res; } /** * 根據key刪除數據 */ public boolean del(final String key) { boolean res = false; try { template.delete(key); res = true; } catch (Exception e) { e.printStackTrace(); } return res; } /** * 是否存在key */ public boolean hasKey(final String key) { boolean res = false; try { res = template.hasKey(key); } catch (Exception e) { e.printStackTrace(); } return res; } /** * 給指定的key設置存活時間 * 默認為-1,表示永久不失效 */ public boolean setExpire(final String key, long seconds) { boolean res = false; try { if (0 < seconds) { res = template.expire(key, seconds, TimeUnit.SECONDS); } } catch (Exception e) { e.printStackTrace(); } return res; } /** * 獲取指定key的剩余存活時間 * 默認為-1,表示永久不失效,-2表示該key不存在 */ public long getExpire(final String key) { long res = 0; try { res = template.getExpire(key, TimeUnit.SECONDS); } catch (Exception e) { e.printStackTrace(); } return res; } /** * 移除指定key的有效時間 * 當key的有效時間為-1即永久不失效和當key不存在時返回false,否則返回true */ public boolean persist(final String key) { boolean res = false; try { res = template.persist(key); } catch (Exception e) { e.printStackTrace(); } return res; } }
最后,我們可以使用單元測試來檢測我們在RedisUtil中寫的操作Redis數據庫的方法
package com.zyxx.test; import com.zyxx.test.utils.RedisUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class) @SpringBootTest public class TestApplicationTest { @Resource private RedisUtil redisUtil; @Test public void setRedis() { boolean res = redisUtil.set("jay", "周杰倫 - 《以父之名》"); System.out.println(res); } @Test public void getRedis() { String res = redisUtil.get("jay"); System.out.println(res); } @Test public void updateRedis() { boolean res = redisUtil.update("jay", "周杰倫 - 《夜的第七章》"); System.out.println(res); } @Test public void delRedis() { boolean res = redisUtil.del("jay"); System.out.println(res); } @Test public void hasKey() { boolean res = redisUtil.hasKey("jay"); System.out.println(res); } @Test public void expire() { boolean res = redisUtil.setExpire("jay", 100); System.out.println(res); } @Test public void getExpire() { long res = redisUtil.getExpire("jay"); System.out.println(res); } @Test public void persist() { boolean res = redisUtil.persist("jay"); System.out.println(res); } }推薦使用Redis客戶端(redis-desktop-manager)來查看Redis數據庫中的數據至此,我們在日常項目中整合Redis的基本使用操作就完成了,但在實際項目中,可能會涉及到更復雜的用法,可以根據你的業務需求調整Redis的使用即可。
到此這篇關于SpringBoot中整合Redis實現熱點數據緩存的文章就介紹到這了,更多相關SpringBoot熱點數據緩存內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
關鍵詞: