• springboot使用redis


    1、pom文件中引入 spring-boot-starter-redis

    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-redis</artifactId>  
    </dependency>  

    2、添加application.properties配置文件

    # REDIS (RedisProperties)
    # Redis数据库索引(默认为0)
    spring.redis.database=0  
    # Redis服务器地址
    spring.redis.host=192.168.0.58
    # Redis服务器连接端口
    spring.redis.port=6379  
    # Redis服务器连接密码(默认为空)
    spring.redis.password=  
    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.pool.max-active=8  
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.pool.max-wait=-1  
    # 连接池中的最大空闲连接
    spring.redis.pool.max-idle=8  
    # 连接池中的最小空闲连接
    spring.redis.pool.min-idle=0  
    # 连接超时时间(毫秒)
    spring.redis.timeout=1000
    

    这里的连接超时时间为0的话,运行程序会报连接超时异常

    3.使用redisTemplate来操作redis中的数据

    @Test
        public void testObj() throws Exception {
            User user=new User(111,"user01","user01");
            ValueOperations<String, User> operations=redisTemplate.opsForValue();
            operations.set("user", user);
            operations.set("user2", user);
            boolean exists=redisTemplate.hasKey("user");
            if(exists){
                System.out.println("exists is true");
            }else{
                System.out.println("exists is false");
            }
            operations.set("2",new User(111,"aa","zz"));
            System.out.println(operations.get("2"));
            User user1 = operations.get("user");
            User user2 = operations.get("user2");
            System.out.println(user1 + "," + user2.toString());
            // Assert.assertEquals("aa", operations.get("com.neo.f").getUserName());
        }

    设置过期时间

    redistempalate的超时设置时,一定要每次用set写入时,更新超时,不然默认会失效的。
    例如:
    //获取过期时间
    int tempTime = this.redisTemplate.getExpire("max").intValue();
    //获取value
    tempCount = this.redisTemplate.opsForValue().get("max");
    //设置key-value
    this.redisTemplate.opsForValue().set("max", tempCount);
    //设置过期时间
    this.redisTemplate.expire("max",tempTime,TimeUnit.SECONDS);

    通过Jedis操作redis

    这里在pom文件中引入依赖

    <!--Jedis的jar-->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    

      

    示例

    //连接本地的 Redis 服务
            Jedis jedis = new Jedis("ip地址",端口);
            jedis.auth("mypassowrd");
            System.out.println("连接成功");
    
            //清空redis
            String s = jedis.flushAll();
            System.out.println(s);
    
            // 获取所有key并输出
            Set<String> keys = jedis.keys("*");
            Iterator<String> it=keys.iterator() ;
            while(it.hasNext()){
                String key = it.next();
                System.out.println(key);
            }
    

      

  • 相关阅读:
    Action直接访问Servlet API
    Struts2与Struts1的对比
    参入github上的开源项目的步骤
    TortoiseSVN中图标的含义
    eclipse 快捷键
    base(function strchr)
    1
    Java 关于路径
    java初阶
    关于C++中计时的方法
  • 原文地址:https://www.cnblogs.com/sueyyyy/p/10006264.html
Copyright © 2020-2023  润新知