Redis快速入门(整合Springboot+redis基础操作)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、idea新建springboot项目,勾选4个依赖项

在这里插入图片描述

注意:在applicaiton.properties配置文件中配置


Redis服务器地址
spring.redis.host=192.168.12.59

二、项目结构

在这里插入图片描述

三、String操作

package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

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

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@Component
public class StringDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final String key1 = "tea#01";
    private final String key2 = "tea#02";

    public void test() {
        redisTemplate.opsForValue().set(key1, "张三");
        String s = redisTemplate.opsForValue().get(key1);
        System.out.println(s);
    }


    public void test2() throws InterruptedException {
        redisTemplate.opsForValue().set(key2, "李四", 30, TimeUnit.SECONDS);
        String s = redisTemplate.opsForValue().get(key2);
        System.out.println("第一次获取值:" + s);
        Thread.sleep(31 * 1000);
        String s2 = redisTemplate.opsForValue().get(key2);
        System.out.println("第二次获取值:" + s2);
    }

}

单元测试类:

package cn.hncj.springbootredis.demo;

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

import static org.junit.jupiter.api.Assertions.*;

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@SpringBootTest
class StringDemoTest {
    @Autowired
    private StringDemo stringDemo;
    @Test
    void test1() {
        stringDemo.test();
    }

    @Test
    void test2() throws InterruptedException {
        stringDemo.test2();
    }

}

在这里插入图片描述

四、Set操作

package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

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

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@Component
public class SetDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final String key5 = "tea#04";
    private final String key6 = "tea#05";


    public void test() {
        redisTemplate.opsForSet().add(key5,"语文");
        redisTemplate.opsForSet().add(key5,"数学");
        redisTemplate.opsForSet().add(key5,"英语");


        redisTemplate.opsForSet().add(key6,"语文");
        redisTemplate.opsForSet().add(key6,"英语");

        Set<String> intersect = redisTemplate.opsForSet().intersect(key5, key6);
        System.out.println(intersect);

    }


}

单元测试类:

package cn.hncj.springbootredis.demo;

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

import static org.junit.jupiter.api.Assertions.*;

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@SpringBootTest
class SetDemoTest {
    @Autowired
    private SetDemo setDemo;
    @Test
    void test1() {
        setDemo.test();
    }
}

在这里插入图片描述

五、 Hash操作

package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

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

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@Component
public class HashDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private  final  String key3 ="tea#03";

    public void test(){
        redisTemplate.opsForHash().put(key3,"家庭住址","开封");
        redisTemplate.opsForHash().put(key3,"大学","开封大学");
        List<Object> values = redisTemplate.opsForHash().values(key3);

        for (Object value:values){
            System.out.println(value);
        }
    }
}

package cn.hncj.springbootredis.demo;

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

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@SpringBootTest
class HashDemoTest {
    @Autowired
    private HashDemo hashDemo;

    @Test
    void test1() {

        hashDemo.test();
    }
}

在这里插入图片描述

六、List操作

package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@Component
public class ListDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final String key1="tea#07";
    public void test(){
    redisTemplate.opsForList().leftPush(key1,"张三");
    redisTemplate.opsForList().leftPush(key1,"李四");
    redisTemplate.opsForList().leftPush(key1,"王五");
        Object o = redisTemplate.opsForList().rightPop(key1);
        System.out.println(o);
    }
}

package cn.hncj.springbootredis.demo;

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

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@SpringBootTest
class ListDemoTest {
    @Autowired
    private ListDemo listDemo;

    @Test
    void test1() {

        listDemo.test();
    }
}

在这里插入图片描述

七、ZSet操作

package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@Component
public class ZSetDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final String key6 = "tea#06";


    public void test() {
        redisTemplate.opsForZSet().add(key6, "语文", 88);
        redisTemplate.opsForZSet().add(key6, "数学", 99);
        redisTemplate.opsForZSet().add(key6, "英语", 66);

        ZSetOperations.TypedTuple<String> stringTypedTuple = redisTemplate.opsForZSet().popMax(key6);
        System.out.println("成绩最高的科目:"+stringTypedTuple.getValue());
        System.out.println("成绩最高的科目:"+stringTypedTuple.getScore());
    }


}

package cn.hncj.springbootredis.demo;

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

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@SpringBootTest
class ZSetDemoTest {
    @Autowired
    private ZSetDemo zsetDemo;
    @Test
    void test1() {
        zsetDemo.test();
    }
}

在这里插入图片描述

八、 BitMap操作

package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@Component
public class BitDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private  final  String key3 ="tea#08";

    public void test(){
        redisTemplate.opsForValue().setBit(key3,2,true);
        redisTemplate.opsForValue().setBit(key3,144,true);

        RedisCallback<Long> callback=connection -> {
            return connection.bitCount(key3.getBytes(),0,19);
        };
        Long totalCount = redisTemplate.execute(callback);
        System.out.println(totalCount);

    }
}

package cn.hncj.springbootredis.demo;

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

/**
 * Created on 2022/5/26.
 *
 * @author Hou chaof
 */
@SpringBootTest
class BitMapDemoTest {
    @Autowired
    private BitDemo bitDemo;

    @Test
    void test1() {

        bitDemo.test();
    }
}

在这里插入图片描述

查看终端工具:在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值