redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)

平常测试 redis 操作命令,可能用的是 cmd 窗口 操作 redis,记录一下 java 程序操作 reids,

 操作 redis 的方法 可以用 Jedis ,在 springboot 提供了两种 方法操作 RedisTemplate 和 StringRedisTemplate 两种方法的区别 可参考:https://blog.csdn.net/yifanSJ/article/details/79513179

 当然 springboot 中也可以使用 Jedis ,本次记录的是 如何使用 RedisTemplate 操作 reids 的 5 种基本数据类型

  1.  在项目的 pom 文件中导入 操作 redis 所需要的依赖:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.2.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>springboot-redis</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot-redis</name>
        <description>Demo project for Spring Boot</description>
    
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">java.version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.8<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">java.version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-data-redis<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-web<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql-connector-java<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>runtime<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> https://mvnrepository.com/artifact/org.apache.directory.studio/org.apache.commons.lang </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.apache.directory.studio<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.apache.commons.lang<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>2.6<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>redis.clients<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>jedis<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> &lt;version&gt;2.9.0&lt;/version&gt; </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-maven-plugin<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    

    </project>

     

  2. 新建 RedisTemplateUtil  注入 RedisTemplate 
    package com.example.demo.redisutil;
    

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;

    import javax.annotation.Resource;

    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    import org.springframework.util.CollectionUtils;

    @Component
    public class RedisTemplateUtil {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

     

  3. 使用 RedisTemplate  操作 String 类型的数据:
      1, 将 String 类型存入到 Redis 当中:
    /**
         * redisTemplate 操作普通字符串 (存值)
         * 
         * @param key
         * @param value
         */
        public void redisSetString(String key, String value) {redisTemplate.opsForValue().set(key, value);
        }

    2, 从 redis 当中取出 String 类型的数据:

    /**
         * redisTemplate 操作普通字符串 (取值)
         * 
         * @param key
         */
        public Object redisGetString(String key) {
            return redisTemplate.opsForValue().get(key);
    
    }</span></pre>
    

    3. 设置 reids key 的过期时间 (原来出现过设置过期时间不起作用,是因为先设置了过期时间 在去存入值到 redis):

        /**
         * 
         * 指定缓存失效时间
         * 
         * @param key
         *            键
         * 
         * @param time
         *            时间 (秒)
         * 
         * @return
         * 
         */
    
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> expire(String key, <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> time) {
        </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (time &gt; 0<span style="color: rgba(0, 0, 0, 1)">) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
    
            e.printStackTrace();
    
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
    
        }
    
    }</span></pre>
    

     

  4. 使用 RedisTemplate  操作 List 类型的数据:
     1, 将 list 类型存入到 Redis 当中:
        /**
         * 将一个 list 集合存放到 redis 当中
         * 
         * @param key
         */
        public void redisSetList(String key) {
            List<Integer> list = Arrays.asList(9, 2, 3, 4);
            for (Integer integer : list) {
                // 从当前的数据 向右添加 
                // redisTemplate.opsForList().rightPush(key, integer);
                // 从当前的数据 向左添加 
                redisTemplate.opsForList().leftPush(key, integer);
            }
        }

    2, 从 redis 中取出 list 数据:

    /**
         * 获取 list(获取 0 -10 索引的数据)
         * 
         * @param key
         * @return
         */
        public Object getList(String key) {
            return redisTemplate.opsForList().range(key, 0, 10);}

    3. 获取 redis 中 list 的长度:

    /**
         * 获取 list 指定 key 的长度
         * 
         * @param key
         * @return
         */
        public Long getListSize(String key) {
            return redisTemplate.opsForList().size(key);
        }

     


  5. 使用 RedisTemplate  操作 hash 类型的数据:
    1. 将 hash 存放到 redis 当中:
    /**
         * 将 map 存放到 reids
         *
         * @param key
         */
        public void setHash(String key) {
            Map<String, String> hashMap = new HashMap<>();
            //使用 RedisTemplate  有些情况会乱码
            hashMap.put("redis", "redis");
            hashMap.put("mysql", "mysql");
            for (Entry<String, String> keyValue : hashMap.entrySet()) {redisTemplate.opsForHash().put(key, keyValue.getKey(), keyValue.getValue());}
        }

    2. 将 hash 从 redis 当中取出来 根据具体的 key 取出具体的值:

    /**
         * 获取指定 key1 的值
         * 
         * @param key
         * @param key1
         * @return
         */
        public Object getHash(String key, String key1) {
            // 检测 是否 存在该键
            boolean isKey = redisTemplate.opsForHash().hasKey(key, key1);
            return redisTemplate.opsForHash().get(key, key1);
        }

    3. 将 key 中所有的值都取出来:

    /**
         * 获取指定 key 的所有值
         * 
         * @param key
         * 
         * @return
         */
        public Object getHash(String key) {
            return redisTemplate.opsForHash().entries(key);
        }

    4. 根据具体的 key 移除具体的值:

    /**
         * 根据具体 key 移除具体的值
         * 
         * @param key
         * 
         * @return
         */
        public void removeKey(String key, String key1) {redisTemplate.opsForHash().delete(key, key1);
        }

    5. 移除 key 值 则 key 里面的所有值都被移除:

    public void removeStringKey(String key) {redisTemplate.delete(key);
        }

     

  6. 使用 RedisTemplate  操作 set 类型的数据:
    1.set 数据存入 redis 中
        /**
         * set 存入 redis 中
         * 
         * @param key
         */
        public void setSet(String key) {
            Set<Object> set = new HashSet<>();
            set.add("setKey");
            set.add("tesetKey");
            for (Object object : set) {redisTemplate.opsForSet().add(key, object);
            }
        }

    2. 从 redis 当中将 set 数据取出来:

    /**
         * 从 redis 中取出 set
         * 
         * @param key
         * @return
         */
        public Object getSet(String key) {
            return redisTemplate.opsForSet().members(key);
        }

     

  7. 使用 RedisTemplate  操作 sortset 类型的数据:
    1. 将 sortSet 存放到 redis 当中:
    /**
         * sortset 存入 redis 中
         * 
         * @param key
         */
        public void setZSet(String key) {
            Set<Object> set = new HashSet<>();
            set.add("setKey");
            set.add("tesetKey");
            int i = 0;
            for (Object object : set) {
                i++;
                redisTemplate.opsForZSet().add(key, object, i);
            }
        }

    2. 将 sortset 从 redis 当中取出来的 API 有很多:

    /**
         * 从 redis 中取出 sortset
         * 
         * @param key
         * @return
         */
        public Object getZSet(String key) {
            Long size = redisTemplate.opsForZSet().size(key);
            return redisTemplate.opsForZSet().rangeByScore(key, 0, size);}

     

  8. RedisTemplate 操作 5 种基本类型数据,有一些共同的 API 比如设置过期时间,和移除 key 的值, 判断 key 是否存在
    1 设置过期时间
    /**
         * 
         * 指定缓存失效时间
         * 
         * @param key
         *            键
         * 
         * @param time
         *            时间 (秒)
         * 
         * @return
         * 
         */
    
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> expire(String key, <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> time) {
        </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (time &gt; 0<span style="color: rgba(0, 0, 0, 1)">) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
    
            e.printStackTrace();
    
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
    
        }
    
    }</span></pre>
    

    2. 检测是否存在 key

    /**
         * 
         * 判断 key 是否存在
         * 
         * @param key
         *            键
         * 
         * @return true 存在 false 不存在
         * 
         */
    
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean checkKey</span><span style="color: rgba(0, 0, 0, 1)">(String key) {
    
        </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
    
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> redisTemplate.hasKey(key);
    
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
    
            e.printStackTrace();
    
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
    
        }
    
    }</span></pre>
    

    3: 移除缓存

      /**
         * 移除 key
         * @param key
         */
        public void removeKey(String key) {redisTemplate.delete(key);
        }

     


  9. 掌握 redis,首先得掌握 reids 如何操作 5 种基本数据类型,5 种基本数据类型中 set 和 sortset 的区别需要特别注意,然后就是关于使用 RedisTemplate  可能乱码的问题,以及 StringRedisTemplate  的区别,