栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot使用redis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot使用redis

springboot使用redis



下载并启动redis

下载redis

点我下载


运行redis






编写代码

pom.xml 导入依赖


 
    org.springframework.boot
    spring-boot-starter-data-redis




编写工具类

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 redisTemplate;


    
    public String get(final String key){
        return  redisTemplate.opsForValue().get(key);
    }


    
    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;
    }



    
    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;
    }


    
    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);
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777934.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号