1.新建一個SpringBoot項目,選擇WEB選項。
2.pom文件增加依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </dependency>
3.redis配置,這里密碼為空,所以password沒有賦值
# Redis數據庫索引(默認為0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host=127.0.0.1
# Redis服務器連接端口
spring.redis.port=6379
# Redis服務器連接密碼(默認為空)
spring.redis.password=
# 連接池最大連接數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-active=8
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=-1
# 連接池中的最大空閑連接
spring.redis.jedis.pool.max-idle=8
# 連接池中的最小空閑連接
spring.redis.jedis.pool.min-idle=0
# 連接超時時間(毫秒)
spring.redis.timeout=0
4.編寫緩存配置類
@Configuration @EnableCaching public class RedisCacheConfiguration extends CachingConfigurerSupport { Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class); @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.max-wait}") private long maxWaitMillis; @Value("${spring.redis.password}") private String password; @Bean(name = "jedisPool") public JedisPool redisPoolFactory() { logger.info("JedisPool注入成功!!"); logger.info("redis地址:" + host + ":" + port); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); JedisPool jedisPool = null; if(password == null || password.equals("")){ jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout); }else{ jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password); } return jedisPool; } }
上面的判斷用來支持密碼為空和不微空的兩種情況
5.編寫緩存工具類
@Service @Lazy(false) public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); /** * 取得存儲在靜態變量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } /** * 從靜態變量applicationContext中取得Bean, 自動轉型為所賦值對象的類型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); } /** * 從靜態變量applicationContext中取得Bean, 自動轉型為所賦值對象的類型. */ public static <T> T getBean(Class<T> requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } /** * 清除SpringContextHolder中的ApplicationContext為Null. */ public static void clearHolder() { if (logger.isDebugEnabled()){ logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext); } applicationContext = null; } /** * 實現DisposableBean接口, 在Context關閉時清理靜態變量. */ @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); } /** * 檢查ApplicationContext不為空. */ private static void assertContextInjected() { //Validate.validState(applicationContext != null, "applicaitonContext屬性未注入, 請在applicationContext.xml中定義SpringContextHolder."); } /* (non-Javadoc) * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextHolder.applicationContext = applicationContext; } }
public class JedisUtils { private static JedisPool jedisPool = SpringContextHolder.getBean("jedisPool"); public static void set(String key, String value) { Jedis jedis = jedisPool.getResource(); jedis.set(key, value); } public static String get(String key) { Jedis jedis = jedisPool.getResource(); return jedis.get(key); } }
6.測試
@RestController public class TestController { @GetMapping("/testPutJedis") public String testPutJedis() { JedisUtils.set("test", "Redis"); return "hi, I'm Jedis,OK !"; } @GetMapping("/testGetJedis") public String testGetJedis() { JedisUtils.get("test"); return JedisUtils.get("test"); } }
可以發現輸入127.0.0.1:8080/testPutJedis后,緩存被放入,輸入127.0.0.1:8080/testGetJedis后,緩存數據被查出來