SpringBoot項目中,Redis的初次使用


1.引入Redis依賴包,在application.yml中配置redis

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
Spring:
  redis:
    host: 192.168.1.105
    port: 6379
    password: 

 

2.引入Redis模板,這里我只使用了,StringRedisTemplate

    @Autowired
    private StringRedisTemplate redisTemplate;

 

3.數據存入Redis中

        String token = UUID.randomUUID().toString();
        Integer expire = RedisConstant.EXPORE;
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX, token), openid, expire, TimeUnit.SECONDS);
redisTemplate.opsForValue().set()方法中
第一個參數:存放的數據的名稱String.format(RedisConstant.TOKEN_PREFIX, token)
第二個參數:存放的內容:openid
第三個參數:存放的時間:expire
第四個參數:存放的格式:TimeUnit.SECONDS

4.在Redis中查詢內容

 

 

 

5.注銷Redis中內容

redisTemplate.opsForValue().getOperations().delete((String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue())));
redisTemplate.opsForValue().getOperations().delete()
第一個參數:注銷的內容的名稱(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()))

借用了redis的桌面可視化工具方便查看數據

 

 

 







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM