Installation tutorial: How to install Redis under Widows?
1. Start the redis service first
2. Import prm.xml
<dependencies> <!--redis dependency--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.7.0</version> </dependency> </dependencies>
3. Configure yml
spring: #Configure reids redis: host: 127.0.0.1 #Local IP address port: 6379 #reids service port number
4, reids configuration class
** * redis configuration class */ @Configuration public class RedisConfiguration { /** * springboot2.x uses LettuceConnectionFactory instead of RedisConnectionFactory * After application.yml configures basic information, springboot2.x RedisAutoConfiguration can be automatically assembled * LettuceConnectionFactory and RedisConnectionFactory and their RedisTemplate * @param redisConnectionFactory * @return */ @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory){ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); // Add serializers as needed // template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); // The key adopts the serialization method of String redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }
5, redis tool class
/** * Redis tool class */ @Component public final class RedisUtil { @Resource private RedisTemplate<String, Object> redisTemplate; // ============================== common=================== ========== /** * Specifies the cache expiration time * @param key key * @param time time (seconds) * @return */ public boolean expire(String key, long time) { try { if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Get the expiration time according to the key * @param key key cannot be null * @return time (seconds) return 0 means it is permanent */ public long getExpire(String key) { return redisTemplate.getExpire(key, TimeUnit.SECONDS); } /** * Determine whether the key exists * @param key key * @return true exists false does not exist */ public boolean hasKey(String key) { try { return redisTemplate.hasKey(key); } catch (Exception e) { e.printStackTrace(); return false; } } /** * delete cache * @param key can pass one value or more */ @SuppressWarnings("unchecked") public void del(String... key) { if (key != null & amp; & amp; key. length > 0) { if (key. length == 1) { redisTemplate.delete(key[0]); } else { redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key)); } } } // =============================String==================== ========== /** * Ordinary cache acquisition * @param key key * @return value */ public Object get(String key) { return key == null ? null : redisTemplate.opsForValue().get(key); } /** * Ordinary cache put * @param key key * @param value value * @return true success false failure */ public boolean set(String key, Object value) { try { redisTemplate.opsForValue().set(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Ordinary cache is put in and set time * @param key key * @param value value * @param time time (seconds) time must be greater than 0, if time is less than or equal to 0, it will be set indefinitely * @return true success false failure */ public boolean set(String key, Object value, long time) { try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { set(key, value); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * increment * @param key key * @param delta How much to increase (greater than 0) * @return */ public long incr(String key, long delta) { if (delta < 0) { throw new RuntimeException("Increment factor must be greater than 0"); } return redisTemplate.opsForValue().increment(key, delta); } /** * decrement * @param key key * @param delta how much to reduce (less than 0) * @return */ public long decr(String key, long delta) { if (delta < 0) { throw new RuntimeException("The decrement factor must be greater than 0"); } return redisTemplate.opsForValue().increment(key, -delta); } // =================================Map================ =================== /** * HashGet * @param key key cannot be null * @param item item cannot be null * @return value */ public Object hget(String key, String item) { return redisTemplate.opsForHash().get(key, item); } /** * Get all key values corresponding to hashKey * @param key key * @return corresponding multiple key values */ public Map<Object, Object> hmget(String key) { return redisTemplate.opsForHash().entries(key); } /** * HashSet * @param key key * @param map corresponds to multiple key values * @return true success false failure */ public boolean hmset(String key, Map<String, Object> map) { try { redisTemplate.opsForHash().putAll(key, map); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * HashSet and set the time * @param key key * @param map corresponds to multiple key values * @param time time (seconds) * @return true success false failure */ public boolean hmset(String key, Map<String, Object> map, long time) { try { redisTemplate.opsForHash().putAll(key, map); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Put data into a hash table, if it does not exist, it will be created * @param key key * @param item item * @param value value * @return true success false failure */ public boolean hset(String key, String item, Object value) { try { redisTemplate.opsForHash().put(key, item, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Put data into a hash table, if it does not exist, it will be created * @param key key * @param item item * @param value value * @param time Time (seconds) Note: If the existing hash table has time, the original time will be replaced here * @return true success false failure */ public boolean hset(String key, String item, Object value, long time) { try { redisTemplate.opsForHash().put(key, item, value); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Delete the value in the hash table * @param key key cannot be null * @param item item can be multiple and cannot be null */ public void hdel(String key, Object... item) { redisTemplate.opsForHash().delete(key, item); } /** * Determine whether there is a value for this item in the hash table * @param key key cannot be null * @param item item cannot be null * @return true exists false does not exist */ public boolean hHasKey(String key, String item) { return redisTemplate.opsForHash().hasKey(key, item); } /** * hash increment If it does not exist, it will create one and return the newly added value * @param key key * @param item item * @param by how much to increase (greater than 0) * @return */ public double hincr(String key, String item, double by) { return redisTemplate.opsForHash().increment(key, item, by); } /** * hash decrement * @param key key * @param item item * @param by to reduce (less than 0) * @return */ public double hdecr(String key, String item, double by) { return redisTemplate.opsForHash().increment(key, item, -by); } // ================================================= ========== /** * Get all the values in the Set according to the key * @param key key * @return */ public Set<Object> sGet(String key) { try { return redisTemplate.opsForSet().members(key); } catch (Exception e) { e.printStackTrace(); return null; } } /** * Query from a set according to value, whether it exists * @param key key * @param value value * @return true exists false does not exist */ public boolean sHasKey(String key, Object value) { try { return redisTemplate.opsForSet().isMember(key, value); } catch (Exception e) { e.printStackTrace(); return false; } } /** * Put the data into the set cache * @param key key * @param values value can be multiple * @return number of successes */ public long sSet(String key, Object... values) { try { return redisTemplate.opsForSet().add(key, values); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * Put the set data into the cache * @param key key * @param time time (seconds) * @param values value can be multiple * @return number of successes */ public long sSetAndTime(String key, long time, Object... values) { try { Long count = redisTemplate.opsForSet().add(key, values); if (time > 0) expire(key, time); return count; } catch (Exception e) { e.printStackTrace(); return 0; } } /** * Get the length of the set cache * @param key key * @return */ public long sGetSetSize(String key) { try { return redisTemplate.opsForSet().size(key); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * Remove the value of value * @param key key * @param values value can be multiple * @return the number of removed */ public long setRemove(String key, Object... values) { try { Long count = redisTemplate.opsForSet().remove(key, values); return count; } catch (Exception e) { e.printStackTrace(); return 0; } } // ================================= list================== ================== /** * Get the content of the list cache * @param key key * @param start start * @param end end 0 to -1 represent all values * @return */ public List<Object> lGet(String key, long start, long end) { try { return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { e.printStackTrace(); return null; } } /** * Get the length of the list cache * @param key key * @return */ public long lGetListSize(String key) { try { return redisTemplate.opsForList().size(key); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * Get the value in the list by index * @param key key * @param index When index>=0, 0 is the header, 1 is the second element, and so on; when index<0, -1 is the end of the table, -2 is the second-to-last element, and so on * @return */ public Object lGetIndex(String key, long index) { try { return redisTemplate.opsForList().index(key, index); } catch (Exception e) { e.printStackTrace(); return null; } } /** * Put the list into the cache * @param key key * @param value value * @param * @return */ public boolean lSet(String key, Object value) { try { redisTemplate.opsForList().rightPush(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Put the list into the cache * @param key key * @param value value * @param time time (seconds) * @return */ public boolean lSet(String key, Object value, long time) { try { redisTemplate.opsForList().rightPush(key, value); if (time > 0) expire(key, time); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Put the list into the cache * @param key key * @param value value * @param * @return */ public boolean lSet(String key, List<Object> value) { try { redisTemplate.opsForList().rightPushAll(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Put the list into the cache * * @param key key * @param value value * @param time time (seconds) * @return */ public boolean lSet(String key, List<Object> value, long time) { try { redisTemplate.opsForList().rightPushAll(key, value); if (time > 0) expire(key, time); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Modify a piece of data in the list according to the index * @param key key * @param index index * @param value value * @return */ public boolean lUpdateIndex(String key, long index, Object value) { try { redisTemplate.opsForList().set(key, index, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Remove N values as value * @param key key * @param count how many to remove * @param value value * @return the number of removed */ public long lRemove(String key, long count, Object value) { try { Long remove = redisTemplate.opsForList().remove(key, count, value); return remove; } catch (Exception e) { e.printStackTrace(); return 0; } } }
6. Test
@GetMapping("/set") public String set() { redisUtil.set("key", "value"); return "success"; } ? @GetMapping("/get") public String get() { Object result = redisUtil. get("key"); return result. toString(); }