Spring Boot除了對對經常使用的關係型數據庫提供支持之外,還對非關係型數據庫進行了自動化配置。java
使用redis結合spring cache是一個很是棒的組合
cache使用方便,但仍是使用JVM內存了緩存對象
redis是獨立的緩存服務器,使用單純的內存來作緩存
因此他們結合後能夠很方便的進行緩存而且不影響JVM的內存性能redis
接下來我會教你們如何在Spring Boot整合Redis。spring
Redis簡單介紹:
redis是一個開源的的日誌型、key-value鍵值對數據庫。數據庫
它有三個特色:
1.redis將數據存放在內存中,也能夠將數據持久化到磁盤;
2.與其餘鍵值對數據存儲相比,它還有string、hash、set、sortedset、list等數據結構;
3.支持主從複製緩存
redis在Spring Boot上的配置很簡單:服務器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
這裏咱們在application.properties(.yxml也能夠)中進行配置:數據結構
# Redis數據庫索引(默認爲0) spring.redis.database=0 # Redis服務器地址 spring.redis.host=localhost # Redis服務器鏈接端口 spring.redis.port=6379 # Redis服務器鏈接密碼(默認爲空) spring.redis.password= # 鏈接池最大鏈接數(使用負值表示沒有限制) spring.redis.pool.max-active=8 # 鏈接池最大阻塞等待時間(使用負值表示沒有限制) spring.redis.pool.max-wait=-1 # 鏈接池中的最大空閒鏈接 spring.redis.pool.max-idle=8 # 鏈接池中的最小空閒鏈接 spring.redis.pool.min-idle=0 # 鏈接超時時間(毫秒) spring.redis.timeout=0
測試代碼(毫無心義):app
@Autowired private StringRedisTemplate<String,String> stringRedisTemplate; @RequestMapping("/set") @ResponseBody public String set(){ redisTemplate.opsForValue().set("key","value"); return "success"; } @RequestMapping("/get") @ResponseBody public String get(){ return redisTemplate.opsForValue().get("key"); }
這時候你也許會問:怎麼只能存入String,不是說能夠存入複雜的數據結構嗎?
別急,看下面的代碼,我來實現把一個User實體存入redis:maven
@Autowired private RedisTemplate<String,String> redisTemplate; @Resource(name = "redisTemplate") private ValueOperations<String,Object> valueOperations; public void test(){ User user = new User("testName","testPassword"); valueOperations.set("user",user); boolean exist = redisTemplate.hasKey("user"); if(exist){ System.out.println("exist"); }else{ System.out.printf("no exist"); } }
以上例子就能夠把User存入redis中。
除了ValueOperations,還有ListOperations,SetOperations,ZSetOperations,HashOperationsspring-boot
下面我會貼出工具類,方便你們你們使用:
package cm.cn.util; import java.util.Collection; import java.util.Set; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.ListOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.SetOperations; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.stereotype.Component; @Component public final class RedisUtil { @Autowired private RedisTemplate<String, String> redisTemplate; @Resource(name = "redisTemplate") private ValueOperations<String, Object> valueOps; @Resource(name = "redisTemplate") private ListOperations<String, String> listOps; @Resource(name = "redisTemplate") private SetOperations<String, String> setOps; @Resource(name = "redisTemplate") private ZSetOperations<String, String> zsetOps; @Resource(name = "redisTemplate") private HashOperations<String, String, String> hashOps; public void setValue(String key, Object value) { valueOps.set(key, value); } public Object getValue(String key) { return valueOps.get(key); } //返回值爲設置成功的value數 public Long setSet(String key, String... value) { return setOps.add(key, value); } //返回值爲redis中鍵值爲key的value的Set集合 public Set<String> getSetMembers(String key) { return setOps.members(key); } public Boolean setZSet(String key, String value, double score) { return zsetOps.add(key, value, score); } public Double getZSetScore(String key, String value) { return zsetOps.score(key, value); } public void deleteKey(String key) { redisTemplate.delete(key); } public void deleteKeys(Collection<String> keys) { redisTemplate.delete(keys); } }
以上即是Redis在SpringBoot上的使用。
以爲還能夠的請點個贊,贊不了也能夠收藏下
還有更精彩的文章呈現給你們,請關注做者
謝謝閱讀~