• redis详解


    redis数据结构

    Redis采用的是键值对存储,(key)键的类型只能为字符串,(value)值支持五种数据类型:

    • 字符串:String
    • 哈希:HashMap
    • 双向链表:LinkedList
    • 无序集合:HashSet
    • 有序集合:LinkedHashSet(zset)
    1.String字符串
    • 新增
      set key value
    • 查询
      get key
    • 删除
      del key
    • 新增的时候设置过期时间(验证码)
      setex key second value
    • 查看剩余时间
      ttl key
    • 根据键判断记录是否存在
      exists key 0:不存在 1:存在
    • 自增(减)操作:
      incr/decr key
    2.Hash哈希

    Hash类型极其类似于java中的Map,值里面可以存放一组组的键值对

    该类型非常适合于存储java中对象的信息

    • 新增
      hset key hkey hvalue
    • 查询
      ----所有
      hgetall key
      ----单个
      hget key hkey
    • 删除
      ----单个
      hdel key hkey
      ----所有
      del key
    • 获取所有hkey
      hkeys key
    • 获取所有hvalue
      hvals key

    Jedis的使用

    API
    方法解释
    new Jedis(host, port)创建jedis对象,参数host是redis服务器地址,参数port是redis服务端口
    set(key,value)设置字符串类型的数据
    get(key)获得字符串类型的数据
    hset(key,field,value)设置哈希类型的数据
    hget(key,field)获得哈希类型的数据
    lpush(key,values)设置列表类型的数据
    lpop(key)列表左面弹栈
    rpop(key)列表右面弹栈
    del(key)删除指定的key
    具体步骤

    1.引入依赖

    <dependencies>
        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.9.3version>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.编写测试类

    public class JedisTest {
    
        public static void main(String[] args) {
            // 1.创建连接
            Jedis jedis = new Jedis("127.0.0.1", 6379);
    
            // 2.新增
            jedis.set("1002", "李四");
          	//3.查询
            String value = jedis.get("1002");
            System.out.println(value);
    
            // 4.关闭连接
            jedis.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Jedis连接池

    public class JedisPoolTest {
    
        public static void main(String[] args) {
            // 0.创建连接池配置对象
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(50); // 最大连接数
            config.setMaxIdle(20); // 最大空闲数
            // 1.创建连接池
            JedisPool jedisPool = new JedisPool(config,"127.0.0.1",6379);
            // 2.获取连接
            Jedis jedis = jedisPool.getResource();
            // 3.进行 新增、修改、删除、查询
    
            // 4.归还连接
            jedis.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    SpringDataRedis(Spring整合的redis)

    Spring Data Redis提供了从Spring应用程序轻松配置和访问Redis的功能

    通过RedisTemplate对底层Jedis进行了高度封装,提供了redis各种操作

    • ValueOperations:简单键值对操作 String

    • SetOperations:set类型数据操作 set

    • ZSetOperations:zset类型数据操作 sortedset---->zset

    • HashOperations:针对hash类型的数据操作 hash

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

    使用步骤

    1.引入依赖

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.5version>
    parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
      
      
       <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.添加配置文件application.yml

    spring:
      redis:
        host: localhost
        port: 6379
        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

    3.创建启动类

    @SpringBootApplication
    public class RedisApplication {
        public static void main(String[] args) {
            SpringApplication.run(RedisApplication.class, args);
        }
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    各数据类型通用操作
    // 通用操作
    @Test
    public void test06()throws Exception{
        // 查询所有key
        Set<String> keys = stringRedisTemplate.keys("*");
        for (String key : keys) {
            System.out.println(key);
        }
        // 判断某个key是否存在
        Boolean itheima = stringRedisTemplate.hasKey("1005");
        System.out.println(itheima);
    
        // 判断某个key的类型
        DataType type = stringRedisTemplate.type("1005");
        System.out.println(type.name());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    String类型操作方法
    // 操作字符串类型
    @Test
    public void test01() throws Exception {
        // 获取string操作对象
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
    
        // 存值
        opsForValue.set("city", "北京");
    
        // 取值
         String value = opsForValue.get("city");
         System.out.println(value);
    
        // 存验证码存活5分钟
         opsForValue.set("sms_13700137000", "6375",TimeUnit.SECONDS);
      
       //删除
       redisTemplate.delete("city");
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    hash类型操作方法
    // 操作hash类型
    @Test
    public void test02() throws Exception {
        // 获取hash操作对象
        HashOperations<String, Object, Object> opsForHash = stringRedisTemplate.opsForHash();
    
        // 存值
        //opsForHash.put("1005", "nage", "zhangsan");
        //opsForHash.put("1005", "age", "18");
        //opsForHash.put("1005", "sex", "man");
    
        // 取出年龄
        String age = (String) opsForHash.get("1005", "age");
        System.out.println(age);
        System.out.println("---------------");
        // 取出所有key
        Set<Object> keys = opsForHash.keys("1005");
        for (Object key : keys) {
            System.out.println(key);
        }
        System.out.println("---------------");
        // 取出所有value
        List<Object> values = opsForHash.values("1005");
        for (Object value : values) {
            System.out.println(value);
        }
    
        // 删除
        opsForHash.delete("1005", "name");
    }
    
    • 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
  • 相关阅读:
    Maleimide-PEG-DSPE 474922-22-0 磷脂-聚乙二醇-马来酰亚胺现货供应
    SpringBoot系列之MongoDB分页接口实现
    声明式事务管理案例-转账(xml、注解)
    Mysql索引优化2
    Deadlock found when trying to get lock; try restarting transaction主要要是死锁问题呢怎么解决
    第15章 秒杀商品隔离解决方案
    想考阿里云ACP认证,网上买题库靠谱吗?
    对比扫描结果 ndiff
    海康威视热成像实时测温java - 23版
    等保测评一体机是什么
  • 原文地址:https://blog.csdn.net/q290994/article/details/126238761