pom依賴node
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-redis</artifactId> 4 </dependency>
Spring boot yml配置redis的參數redis
1 spring: 2 # REDIS (RedisProperties) 3 # Redis數據庫索引(默認爲0) 4 redis: 5 database: 0 6 timeout: 0 7 # Redis服務器地址 8 host: 127.0.0.1 9 # Redis服務器鏈接端口 10 port: 6379 11 # Redis服務器鏈接密碼(默認爲空) 12 password: 13 # 鏈接池最大鏈接數(使用負值表示沒有限制) 14 pool: 15 max-active: 8 16 # 鏈接池最大阻塞等待時間(使用負值表示沒有限制) 17 max-wait: -1 18 # 鏈接池中的最大空閒鏈接 19 max-idle: 8 20 # 鏈接池中的最小空閒鏈接 21 min-idle: 0 22 cluster: 23 max-redirects: 10 24 nodes: 127.0.0.1:6080 25 # 鏈接超時時間(毫秒)
Spring boot config redis的模板spring
1 @Configuration 2 @EnableCaching 3 public class RedisCacheConfig { 4 @Bean 5 public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) { 6 CacheManager cacheManager = new RedisCacheManager(redisTemplate); 7 return cacheManager; 8 } 9 10 @Bean 11 public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { 12 RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); 13 redisTemplate.setConnectionFactory(factory); 14 // key序列化方式;(否則會出現亂碼;),可是若是方法上有Long等非String類型的話,會報類型轉換錯誤; 15 // 因此在沒有本身定義key生成策略的時候,如下這個代碼建議不要這麼寫,能夠不配置或者本身實現ObjectRedisSerializer 16 // 或者JdkSerializationRedisSerializer序列化方式; 17 RedisSerializer<String> redisSerializer = new StringRedisSerializer();// Long類型不能夠會出現異常信息; 18 redisTemplate.setKeySerializer(redisSerializer); 19 redisTemplate.setHashKeySerializer(redisSerializer); 20 return redisTemplate; 21 } 22 23 }
spring boot 的redis 工具類數據庫
1 @SuppressWarnings("unchecked") 2 @Component 3 public class RedisUtil { 4 @SuppressWarnings("rawtypes") 5 @Autowired 6 private RedisTemplate redisTemplate; 7 8 /** 9 * 批量刪除對應的value 10 * 11 * @param keys 12 */ 13 public void remove(final String... keys) { 14 for (String key : keys) { 15 remove(key); 16 } 17 } 18 19 /** 20 * 批量刪除key 21 * 22 * @param pattern 23 */ 24 public void removePattern(final String pattern) { 25 Set<Serializable> keys = redisTemplate.keys(pattern); 26 if (keys.size() > 0) 27 redisTemplate.delete(keys); 28 } 29 30 /** 31 * 刪除對應的value 32 * 33 * @param key 34 */ 35 public void remove(final String key) { 36 if (exists(key)) { 37 redisTemplate.delete(key); 38 } 39 } 40 41 /** 42 * 判斷緩存中是否有對應的value 43 * 44 * @param key 45 * @return 46 */ 47 public boolean exists(final String key) { 48 return redisTemplate.hasKey(key); 49 } 50 51 /** 52 * 讀取緩存 53 * 54 * @param key 55 * @return 56 */ 57 public String get(final String key) { 58 Object result = null; 59 redisTemplate.setValueSerializer(new StringRedisSerializer()); 60 ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 61 result = operations.get(key); 62 if (result == null) { 63 return null; 64 } 65 return result.toString(); 66 } 67 68 /** 69 * 寫入緩存 70 * 71 * @param key 72 * @param value 73 * @return 74 */ 75 public boolean set(final String key, Object value) { 76 boolean result = false; 77 try { 78 ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 79 operations.set(key, value); 80 result = true; 81 } catch (Exception e) { 82 e.printStackTrace(); 83 } 84 return result; 85 } 86 87 /** 88 * 寫入緩存 89 * 90 * @param key 91 * @param value 92 * @return 93 */ 94 public boolean set(final String key, Object value, Long expireTime) { 95 boolean result = false; 96 try { 97 ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 98 operations.set(key, value); 99 redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); 100 result = true; 101 } catch (Exception e) { 102 e.printStackTrace(); 103 } 104 return result; 105 } 106 107 public boolean hmset(String key, Map<String, String> value) { 108 boolean result = false; 109 try { 110 redisTemplate.opsForHash().putAll(key, value); 111 result = true; 112 } catch (Exception e) { 113 e.printStackTrace(); 114 } 115 return result; 116 } 117 118 public Map<String, String> hmget(String key) { 119 Map<String, String> result = null; 120 try { 121 result = redisTemplate.opsForHash().entries(key); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 } 125 return result; 126 } 127 }
基本測試類以下:緩存
1 @RunWith(SpringRunner.class) 2 @SpringBootTest 3 public class RedisUtilTest { 4 @Autowired 5 private RedisUtil redisUtil; 6 7 @Test 8 public void testSet() { 9 String key = "key"; 10 System.out.println(redisUtil.get(key)); 11 String value = "sdd"; 12 redisUtil.set(key, value); 13 System.out.println(redisUtil.get(key)); 14 } 15 }
如今這樣就基本可以使用redis了springboot