11-Redis

1.下载redis最新windows版安装包

从官网下载redis

  https://jingyan.baidu.com/article/63acb44a2ca17461fcc17e82.html

安装redis

解压后先配置redis环境变量再cmd输入命令不然会把命令不合法

环境变量配置:

  https://blog.csdn.net/weixin_42423819/article/details/80634918

windows下redis安装步骤:

         https://blog.csdn.net/erlian1992/article/details/54382443

输入命令安装后出现  “已经准备好连接了”,不要关闭对当前cmd,此时打开另一个cmd进行连接(关闭后再打开一个cmd连接就会没有反应)

https://blog.csdn.net/tongtongsong/article/details/80856570

2.在对应安装redis的目录通过cmd启动命令

d:

cd Redis

redis-server redis.windows.conf

此时此cmd不要关闭 打开另一个cmd连接测试

redis-cli.exe -h 127.0.0.1 -p 6379

set myKey abc

get myKey

3.pom.xml中添加redis依赖

<!--redis依赖开始-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--redis依赖结束-->
<!-- spring session 依赖开始-->
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
</dependency>
<!-- spring session 依赖结束-->

4.在application-dev.properties中添加redis配置项

#redis
spring.redis.host=localhost
spring.redis.port=6379
# Redis数据库索引(默认为0)
spring.redis.database=0

5.在component下添加RedisComponent类

package com.study.model.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class RedisComponent {
    private static final Logger logger =  LoggerFactory.getLogger(RedisComponent.class);
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public void set(String key, String value) {
        ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
        if (!this.stringRedisTemplate.hasKey(key)) {
            ops.set(key, value);

            logger.info("set key success");
        } else {
            // 存在则打印之前的value值
            logger.info("this key = " + ops.get(key));
        }
    }

    public String get(String key) {
        logger.info("get key success");
        return this.stringRedisTemplate.opsForValue().get(key);
    }

    public void del(String key) {
        logger.info("del key success");
        this.stringRedisTemplate.delete(key);
    }
}

6.  添加测试用例

package com.study.model.test;

import com.study.model.component.RedisComponent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisComponentTest {
    @Autowired
    private RedisComponent redisComponent;

    @Test
    public void set() {
        redisComponent.set("xiao.test", "hello world");
    }

    @Test
    public void get() {
        System.out.println(redisComponent.get("xiao.test"));
    }

    @Test
    public void del() {
        redisComponent.del("xiao.test");
    }
}

7.在UseController中增加getSession方法

@ApiOperation(value="获取用户session", notes="获取用户session")
@RequestMapping(value="/session",method = RequestMethod.GET)
public String getSession(HttpSession session){
    UUID uid = (UUID) session.getAttribute("uuid");
    if (uid == null) {
        uid = UUID.randomUUID();
    }
    session.setAttribute("uuid", uid);
    return session.getId();
}

8.测试调用http://localhost:8080/user/session

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值