SpringBoot(七) 整合redis
阿新 • • 發佈:2018-12-11
1、在pom檔案中新增如下配置
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、在application.properties中新增如下的配置
#redis spring.redis.host= spring.redis.port=6379 spring.redis.password= spring.redis.database=1 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.pool.max-idle=500 spring.redis.pool.min-idle=0 spring.redis.timeout=0
3、在啟動上加上啟動快取的註解
@EnableCaching
4、使用redis快取
4.1 直接使用springboot的redisTemplet物件(注意這種方法注入RedisTemplate物件的時候需要使用@Resource進行注入) 主要程式碼如下
import org.springframework.data.redis.core.RedisTemplate; @Resource private RedisTemplate<String, Object> redisTemplate; // 存取字串 redisTemplate.opsForValue().set("a", "1"); Object a = redisTemplate.opsForValue().get("a"); System.out.println(a); redisTemplate.opsForValue().set("b", "勞斯萊斯是"); Object b = redisTemplate.opsForValue().get("b"); System.out.println(b); // 存取user物件 Student stu = new Student(); stu.setId("111"); stu.setName("浙江安吉三年"); stu.setScore("244"); redisTemplate.opsForValue().set("student", stu); Student student = (Student) redisTemplate.opsForValue().get("student"); System.out.println(student);
4.2 使用RedisUtil封裝類
4.2.1 RedisUtil程式碼如下
package com.zw.util; import java.io.Serializable; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Component; /** * * @Description: spring boot 的redis工具類 */ @SuppressWarnings("unchecked") @Component public class RedisUtil { @Autowired private RedisTemplate redisTemplate; /** * 批量刪除對應的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 批量刪除key * * @param pattern */ public void removePattern(final String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } /** * 刪除對應的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判斷快取中是否有對應的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 讀取快取 * * @param key * @return */ public String get(final String key) { Object result = null; redisTemplate.setValueSerializer(new StringRedisSerializer()); ValueOperations<Serializable, Object> operations = redisTemplate .opsForValue(); result = operations.get(key); if (result == null) { return null; } return result.toString(); } /** * 寫入快取 * * @param key * @param value * @return */ public boolean set(final String key, String value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate .opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 寫入快取 * * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate .opsForValue(); operations.set(key, value); redisTemplate.opsForValue().set("b", "勞斯萊斯是"); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public boolean hmset(String key, Map<String, String> value) { boolean result = false; try { redisTemplate.opsForHash().putAll(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public Map<String, String> hmget(String key) { Map<String, String> result = null; try { result = redisTemplate.opsForHash().entries(key); } catch (Exception e) { e.printStackTrace(); } return result; } }
4.2.2 呼叫RedisUtil物件操作redis 主要程式碼如下
@Autowired
private RedisUtil redisUtil;
//注意在呼叫junit的時候這裡一定要注入,不能直接new物件出來
System.out.println("存在aa嗎?"+redisUtil.exists("aa"));
System.out.println("aa的值"+ redisUtil.get("aa"));
redisUtil.set("aa", "張三");
redisUtil.set("ab", "李四", 10L);
Map< String, String> map = new HashMap<String, String>();
map.put("ac", "王五");
map.put("ad", "趙六");
redisUtil.hmset("ac", map);
redisUtil.remove("aa");