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上的使用。
以为还能够的请点个赞,赞不了也能够收藏下
还有更精彩的文章呈现给你们,请关注做者
谢谢阅读~