redis简单快速入门

文章介绍了如何启动Redis服务器,使用redis-cli设置键值,以及在SpringBoot应用中整合Redis,包括配置YML文件,使用RedisTemplate和StringRedisTemplate进行键值对的存取操作,还展示了测试用例进行验证。
摘要由CSDN通过智能技术生成

简单介绍

在这里插入图片描述

启动redis

使用命令redis-server.exe redis.windows.conf

在这里插入图片描述

 连接redis服务,redis-cli,设置Key值 然后取出key值,

在这里插入图片描述

redis清屏是clear

 哈希存储结构,一个key对应多个key 然后每一个key对应一个value

在这里插入图片描述

SpringBoot整合redis

勾选依赖配置
在这里插入图片描述

导入yml配置

spring:
  redis:
    host: localhost
    port: 6379

测试代码.记得注入redisTemplate


package com.ustc;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

@SpringBootTest
class Redis1ApplicationTests {

    @Autowired
    // 操纵redis
    private RedisTemplate redisTemplate;

    @Test
    void contextLoads() {
        System.out.println("fvcnsdjufhviuds");
    }
    
    @Test
    void set(){
//        存储一个值
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("age",11);
    }

    @Test
    void get(){
//        取出这个值
        ValueOperations ops = redisTemplate.opsForValue();
        Object age = ops.get("age");
        System.out.println(age);

    }

    

}

测试hashSet

   @Test
    void hset(){
        HashOperations ops = redisTemplate.opsForHash();
        ops.put("info","b","bb");
    }
    

    @Test
    void hget(){
        HashOperations ops = redisTemplate.opsForHash();
        Object val = ops.get("info", "b");
        System.out.println(val);
    }


使用它StringReidsTemplate

RedisTemplate 以对象作为key和value 内部对数据进行序列化

package com.ustc;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

@SpringBootTest
public class Test1 {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void get(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String name = ops.get("name");
        System.out.println(name);


    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值