0
点赞
收藏
分享

微信扫一扫

springboot使用redis

覃榜言 2022-03-23 阅读 47

springboot使用redis



下载并启动redis

  • 下载redis

    点我下载


  • 运行redis
    在这里插入图片描述
    在这里插入图片描述




编写代码

pom.xml 导入依赖


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



  • 编写工具类
package com.keeson.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.concurrent.TimeUnit;


@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 读取缓存
     * @param key
     * @return
     */
    public String get(final String key){
        return  redisTemplate.opsForValue().get(key);
    }


    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    public  boolean set(final String key ,String value){
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key,value);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }

    public  boolean set(final String key , String value, Integer timeout, TimeUnit timeUnit ){
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key,value,timeout,timeUnit);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }



    /**
     * 更新缓存
     * @param key
     * @param value
     * @return
     */
    public  boolean update(final String key ,String value){
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key,value);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }


    /**
     * 删除缓存
     * @param key
     * @return
     */
    public  boolean delete(final String key ){
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }

    public  boolean delete(final Collection collection ){
        boolean result = false;
        try {
            redisTemplate.delete(collection);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }

}



  • 测试

package com.keeson;

import com.keeson.utils.RedisUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class SpringbootDomeApplicationTests {

    @Autowired
    private RedisUtils redisUtils;

    @Test
    void testRedis() {
        redisUtils.set("name","test");
        String name = redisUtils.get("name");
        System.out.println(name);
    }

    @Test
    void testRedis1() {
        redisUtils.set("name","我的世界",1, TimeUnit.MINUTES);
        String name = redisUtils.get("name");
        System.out.println(name);
    }

}

在这里插入图片描述

举报

相关推荐

0 条评论