Integrating redisTemplate's operation on Redis in SpringBook

Integrating redisTemplate's operation on Redis in SpringBook (2)

List-type operations

1. Add data to the left of the list

    Long leftPush = redisTemplate.opsForList().leftPush("name", name);

2. Add data to the right of the list

    Long rightPush = redisTemplate.opsForList().rightPush("name", name);

3. Add data in batches to the left of the list

ArrayList<Object> list = new ArrayList<>();
    list.add("shanghai");
    list.add("shenzhen");
    list.add("beijing");
    redisTemplate.opsForList().leftPushAll("address", list);

4. The value of the specified index in the query list

    Object name = redisTemplate.opsForList().index("name", index);

5. Values within the specified range of the query list

    List range = redisTemplate.opsForList().range(key, sIndex, eIndex);

6. Modify the value of the specified index in the list

    redisTemplate.opsForList().set(key, index, object);

7. Intercept values within specified ranges in the list

    redisTemplate.opsForList().trim(key, sIndex, eIndex);

8. Remove the specified index and value from the list

    Long remove = redisTemplate.opsForList().remove(key, index, object);

9. Value on the left side of the pop-up list

    Object o = redisTemplate.opsForList().leftPop(key);

10. Value on the right side of the pop-up list

    Object o = redisTemplate.opsForList().rightPop(key, time, TimeUnit.MILLISECONDS);

11. Pop up the value on the right side of the specified list and add the value to the left side of another specified list

    Object o = redisTemplate.opsForList().rightPopAndLeftPush(key1, key2, time, TimeUnit.SECONDS);
Hash-type operations

Before we manipulate hash data types, we also customize the serialization rules

//Define Hash key serialization
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//Define Hash value serialization
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

1. Add key-value

    redisTemplate.opsForHash().put(key,hkey,hvalue);

2. Adding key-value in batches

HashMap<Object, Object> map = new HashMap<>();
map.put("num", 2);
map.put("name", "susu");
map.put("age", 18);
redisTemplate.opsForHash().putAll(key, map);

3. Get all hash values for the specified object

    Map entries = redisTemplate.opsForHash().entries(key);

4. Get all key s of the specified object

    Set keys = redisTemplate.opsForHash().keys(key);

5. Get all value s of the specified object

    List values = redisTemplate.opsForHash().values(key);

6. Get the value of the specified key for the specified object

    Object o = redisTemplate.opsForHash().get(key, hkey);

7. Delete the value value based on the key of the specified object

    Long delete = redisTemplate.opsForHash().delete(key, hkey);

8. Get the length of the value corresponding to the specified key of the specified object

    Long lengthOfValue = redisTemplate.opsForHash().lengthOfValue(key, hkey);

Tags: Programming Redis

Posted on Sun, 06 Oct 2019 10:44:55 -0700 by CrashRoX