• Redis:java和SpringBoot中使用Redis


    Jedis操作Redis6

    Jedis是java开发的操作redis的工具包。

    引入maven依赖:

    <dependency> 
      <groupId>redis.clientsgroupId> 
      <artifactId>jedisartifactId> 
      <version>4.2.1version> 
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用redis的api操作redis:

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPubSub;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    
    public class JedisTest {
    
        Jedis jedis;
    
        @Before
        public void before() {
            this.jedis = new Jedis("192.168.200.129", 6379);
        }
    
        @After
        public void after() {
            //关闭jedis
            this.jedis.close();
        }
    
        /**
         * 测试redis是否连通
         */
        @Test
        public void test1() {
            String ping = jedis.ping();
            System.out.println(ping);
        }
    
        /**
         * string类型测试
         */
        @Test
        public void stringTest() {
            jedis.set("site", "http://www.itsoku.com");
            System.out.println(jedis.get("site"));
            System.out.println(jedis.ttl("site"));
        }
    
        /**
         * list类型测试
         */
        @Test
        public void listTest() {
            jedis.rpush("courses", "java", "spring", "springmvc", "springboot");
            List<String> courses = jedis.lrange("courses", 0, -1);
            for (String course : courses) {
                System.out.println(course);
            }
        }
    
        /**
         *  zset类型测试
         */
        @Test
        public void setTest() {
            jedis.sadd("users", "tom", "jack", "ready");
            Set<String> users = jedis.smembers("users");
            for (String user : users) {
                System.out.println(user);
            }
        }
    
        /**
         *  订阅消息
         *
         * @throws InterruptedException
         */
        @Test
        public void subscribeTest() throws InterruptedException {
            //subscribe(消息监听器,频道列表)
            jedis.subscribe(new JedisPubSub() {
                @Override
                public void onMessage(String channel, String message) {
                    System.out.println(channel + ":" + message);
                }
            }, "sitemsg");
            TimeUnit.HOURS.sleep(1); }
        }
    
        /**
         * 发布消息
         */
        @Test
        public void publishTest() {
            jedis.publish("sitemsg", "hello redis");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    SpringBoot整合Redis

    引入redis的maven配置:

    <dependency> 
      <groupId>org.springframework.bootgroupId> 
      <artifactId>spring-boot-starter-data-redisartifactId> 
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    application.properties中配置redis信息:

    # redis 服务器 ip 
    spring.redis.host=192.168.200.129 
    # redis 服务器端口 
    spring.redis.port=6379 
    # redis 密码 
    #spring.redis.password=root 
    # 连接超时时间(毫秒) 
    spring.redis.timeout=60000 
    # Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
    spring.redis.database=0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用RedisTemplate工具类操作redis:

    springboot中使用RedisTemplate来操作redis,需要在我们的bean中注入这个对象,代码如下:

    @Autowired private RedisTemplate<String, String> redisTemplate; // 用下面5个对象来操作对应的类型 
    this.redisTemplate.opsForValue(); //提供了操作string类型的所有方法 
    this.redisTemplate.opsForList(); // 提供了操作list类型的所有方法 
    this.redisTemplate.opsForSet(); //提供了操作set的所有方法 
    this.redisTemplate.opsForHash(); //提供了操作hash表的所有方法 
    this.redisTemplate.opsForZSet(); //提供了操作zset的所有方法
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    RedisTemplate示例代码:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import java.util.HashMap; import java.util.List;
    import java.util.Map; import java.util.Set;
    import java.util.concurrent.TimeUnit;
    
    
    @RestController
    @RequestMapping("/redis")
    public class RedisController {
    
        @Autowired
        private RedisTemplate<String, String> redisTemplate;
    
        @RequestMapping("/stringTest")
        public String stringTest() {
            this.redisTemplate.delete("name");
            this.redisTemplate.opsForValue().set("name", "路人");
            String name = this.redisTemplate.opsForValue().get("name");
            return name;
        }
    
        @RequestMapping("/listTest")
        public List<String> listTest() {
            this.redisTemplate.delete("names");
            this.redisTemplate.opsForList().rightPushAll("names", "刘德华", "张学友", "郭富城", "黎明");
            List<String> courses = this.redisTemplate.opsForList().range("names", 0, -1);
            return courses;
        }
    
    
        @RequestMapping("setTest")
        public Set<String> setTest() {
            this.redisTemplate.delete("courses");
            this.redisTemplate.opsForSet().add("courses", "java", "spring", "springboot");
            Set<String> courses = this.redisTemplate.opsForSet().members("courses");
            return courses;
        }
    
        @RequestMapping("hashTest")
        public Map<Object, Object> hashTest() {
            this.redisTemplate.delete("userMap");
            Map<String, String> map = new HashMap<>();
            map.put("name", "路人"); map.put("age", "30");
            this.redisTemplate.opsForHash().putAll("userMap", map);
            Map<Object, Object> userMap = this.redisTemplate.opsForHash().entries("userMap");
            return userMap;
        }
    
        @RequestMapping("zsetTest") 
        public Set<String> zsetTest() { 
            this.redisTemplate.delete("languages"); 
            this.redisTemplate.opsForZSet().add("languages", "java", 100d); 
            this.redisTemplate.opsForZSet().add("languages", "c", 95d); 
            this.redisTemplate.opsForZSet().add("languages", "php", 70); 
            Set<String> languages = this.redisTemplate.opsForZSet().range("languages", 0, -1);
            return languages; 
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
  • 相关阅读:
    『现学现忘』Git基础 — 8、Git创建本地版本库
    Pikachu漏洞练习平台之XXE(XML外部实体注入)
    达索智能制造解决方案,敏捷电芯制造如何赋能企业竞争力 | 百世慧®
    kettle连接达梦资源库-达梦资源库初始化SQL
    Vue定义全局组件的三种方式
    Sentinel
    瀚高数据库DML自动转发功能介绍(数据库层面)
    R实现数据分布特征的视觉化——多笔数据之间的比较
    Vue3的Teleport:Teleport是Vue3的一个新功能,它允许我们将子组件渲染到父组件以外的地方,这在处理模态框、弹出窗口等情况时非常有用
    [杂项]从子域名接管到Subtaker
  • 原文地址:https://blog.csdn.net/qq_43745578/article/details/134478482