• 在java中操作Redis


    在java中操作Redis

    Jedis

    Jedis的maven坐标

    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency> 
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.8.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    @Test
    public void testRedis(){
        //1.获取链接
        Jedis jedis=new Jedis("localhost",6379);
    
        //有密码的话
        //jedis.auth("20020630");
        //2.执行具体的操作
            jedis.set("username","xiaoming");//设置
            jedis.del("username");//删除
            
            jedis.hset("myhash","addr","beijin");//存放hash类型的
            jedis.hget("myhash","addr");//获取
            
            jedis.keys("*");//获取所有的key
        //3.关闭连接
        jedis.close();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Spring Data Redis

    准备一个spring boot项目

        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.4.5version>
            <relativePath/>
        parent>
    
        <groupId>com.itheimagroupId>
        <artifactId>springdataredis-demoartifactId>
        <version>1.0-SNAPSHOTversion>
        <properties><java.version>1.8java.version>properties>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-stater-testartifactId>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-stater-data-redisartifactId>
            dependency>
        dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>2.4.5version>
                plugin>
            plugins>
        build>
    project>
    
    • 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

    配置文件

    spring:
      application:
        name: springredis
        
        #redis相关配置
    redis:
      host: localhost
      port: 6379
    #password: 20020630
      database: 0  #0号数据库
      jedis:
        #Redis连接池配置
      pool:
        max-active: 8  #最大连接数
        max-wait: 1ms  #连接池中的最大阻塞等待时间
        max-idle: 4    #连接池中的最大空闲链接
        min-idle: 0    #连接池中的最小空闲链接
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    Redis服务默认提供了16个数据库

    使用select 1/2/3...切换

    Spring Data Redis 中提供了一个高度封装的类:RedisTemplate,针对jedis客户端中大量的api进行了归类封装,将同一类型操作封装为operation接口,具体分类如下

    ValueOperations :简单的K-V操作

    SetOperations:set数据类型的操作

    ZSetOperations:zset类型的数据操作

    HashOperations:针对map类型的数据操作

    ListOperations:针对list类型的数据操作

    在测试类中

    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class SpringDataRedisTest {
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void testString(){
            redisTemplate.opsForValue().set("city","beijin");
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    image-20220808153301792

    需要修改默认的序列化方式

    创建一个配置类继承CachingConfigurerSupport

    @Configuration
    public class RedisConfig extends CachingConfigurerSupport {
    @Bean
        public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
        
        //默认的Key序列化器为:JdkSerializerRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        
        
        
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    五种类型的具体操作

    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class SpringDataRedisTest {
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void testString(){
           //redisTemplate.opsForValue().set("city","beijin");
            Object city = redisTemplate.opsForValue().get("city");
            //设置超时时间10s
            redisTemplate.opsForValue().set("key1","value1",10l, TimeUnit.SECONDS);
            System.out.println(city);
    
        }
    
        //操作hash类型的数据
        @Test
        public void testHash(){
            HashOperations hashOperations = redisTemplate.opsForHash();
            //存值
            hashOperations.put("002","name","zhangsan");
            hashOperations.put("002","age","20");
            //取值
            Object name = hashOperations.get("002", "name");
            System.out.println(name);
    
            //获取hash结构中的所有字段
            Set keys = hashOperations.keys("002");
            for (Object key : keys) {
                System.out.println(key);
            }
    
            //获取hash结构中的所有值
            List values = hashOperations.values("002");
            for (Object value : values) {
                System.out.println(value);
            }
        }
        //操作List类型的数据
        @Test
        public void testList(){
            ListOperations listOperations = redisTemplate.opsForList();
    
    
            //存值
            listOperations.leftPush("mylist","a");
            listOperations.leftPushAll("mylist","b","c");
            //取值
            List<String> mylist = listOperations.range("mylist", 0, -1);
            for (String s : mylist) {
                System.out.println(s);
            }
    
            //出队列
            //获得列表的长度
            Long mylistLengthist = listOperations.size("mylist");
            int mylistLength = mylistLengthist.intValue();
            for (int i = 0; i < mylistLength; i++) {
                System.out.println( listOperations.rightPop("mylist"));
    
            }
    
        }
    
    
        //操作set集合的数据
        @Test
        public void testSet(){
            SetOperations setOperations = redisTemplate.opsForSet();
    
            //存值
            setOperations.add("myset","a","b","c","a");
    
    
            //取值
            Set<String> myset = setOperations.members("myset");
            for (String s : myset) {
                System.out.println(s);
            }
    
            System.out.println("----------------");
            //删除
            setOperations.remove("myset","b");
    
            Set<String> myset1 = setOperations.members("myset");
            for (String s : myset1) {
                System.out.println(s);
            }
    
        }
    
        //ZSET有序集合
        @Test
        public void testZset(){
            ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    
            //存值
            zSetOperations.add("myZset","a",10.0);
            zSetOperations.add("myZset","b",8.0);
            zSetOperations.add("myZset","c",9.0);
            zSetOperations.add("myZset","c",9.0);
            //后来的会覆盖之前相同的元素
    
            //取值
    
            Set<String> myZset = zSetOperations.range("myZset", 0, -1);
    
            for (String s : myZset) {
                System.out.println(s);
            }
            //修改分数
            zSetOperations.incrementScore("myZset","b",20.0);
    
            //删除成员
            zSetOperations.remove("myZset","c");
    
            System.out.println("--------------------");
            Set<String> myZset1 = zSetOperations.range("myZset", 0, -1);
    
            for (String s : myZset1) {
                System.out.println(s);
            }
    
        }
    
        //通用操作
        @Test
        public void testCommon(){
            //获取Redis中的所有的key
    
            Set<String> keys = redisTemplate.keys("*");
            for (String key : keys) {
                System.out.println(key);
            }
            //判断某个key是否存在
            Boolean itcast = redisTemplate.hasKey("itcast");
            System.out.println(itcast);
    
            //删除指定key
            redisTemplate.delete("myZset");
    
            //获取指定key对应的value类型
            DataType myset = redisTemplate.type("myset");
            System.out.println(myset);
    
        }
    }
    
    • 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
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
  • 相关阅读:
    视频生成框架EasyAnimate正式开源!
    Java计算不同时区的时差
    错误方法修改用户名后,开机提示无法登录到你的账户的正确解决方法
    双非大学改考408,软件工程专业考研报考人数较少!
    tese_Time_2h
    Python Django框架的内容管理系统库之wagtail使用详解
    森林防火视频监控及指挥系统解决方案
    Stream学习2
    设计模式从哪来、难学吗、什么时候学、能做什么?(设计模式与开发实践 P1)
    安服-windows&linux日志分析
  • 原文地址:https://blog.csdn.net/qq_57907966/article/details/126262280