redis是一种可基于内存也可基于持久话的日志型、key-value数据库。因为性能高,存储数据类型丰富等优势常被用作数据缓存。
我们利用spring-boot-autoconfiguration.jar包中已有的RedisAutoConfiguration.class类来获取RedisTemplate对象和StringRedisTemplate对象,这样我们只需要在application.yml中增加相应的参数就可以了。
pom.xml :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>1.5.9.RELEASE</version> </dependency>
application.yml :
spring:
redis:
# 数据库索引
database: 0
host: 192.168.100.100
port: 6379
# 连接超时时间(毫秒)
timeout: 30000
pool:
# 连接池最大连接数
max-active: 2000
# 连接池最大阻塞等待时间
max-wait: 6000
# 连接池中的最大空闲连接
max-idle: 300
# 连接池中的最小空闲连接
min-idle: 100
Application:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { private static final Logger LOG = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); app.setWebEnvironment(true); app.run(args); LOG.info("**************** Startup Success ****************"); } }
RedisController:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisController { @Autowired RedisUtils redisUtils; @RequestMapping("/redis") public String login() { redisUtils.set("hello", "world"); String string = redisUtils.get("hello"); return string; } }
RedisUtils:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisUtils { @Autowired private StringRedisTemplate stringRedisTemplate; /** * * 写入缓存<br/> * @param key * @param value * @return */ public boolean set(String key, String value) { try { stringRedisTemplate.opsForValue().set(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * * 写入缓存,设置过期时间<br/> * @param key * @param value * @param time * @param timeUnit * @return */ public boolean set(String key, String value, long time, TimeUnit timeUnit) { try { if (time > 0) { stringRedisTemplate.opsForValue().set(key, value, time, timeUnit); } else { set(key, value); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * * 获取缓存数据<br/> * @param key * @return */ public String get(String key) { return key == null ? "" : stringRedisTemplate.opsForValue().get(key); } /** * * 判断key是否存在<br/> * @param key * @return */ public boolean exists(String key){ boolean result = false; try { result = stringRedisTemplate.hasKey(key); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * 移除key<br/> * @param key * @return */ public boolean remove(String key){ boolean result = false; try { if(exists(key)){ stringRedisTemplate.delete(key); } result = true; } catch (Exception e) { e.printStackTrace(); } return result; } }