mac redis安装及springboot整合redis

一、redis安装

1、直接执行下面的命令

brew install redis

安装成功显示如下

2、启动本地redisserver

brew services start redis

3、看看redis是否启动成功,能否访问

直接在命令窗口依次输入下面的命令

redis-cli
PING

结果如下就是本地redis通了

lxxdeMacBook-Pro:~ lxx$ redis-cli
127.0.0.1:6379> 
127.0.0.1:6379> PING
PONG

二、springboot整合redis

1、在application.yml文件配置redis的ip和端口号

2、Redis自定义注入Bean组件配置:

新建下面的class文件

package com.beauty.time.common;

/**
 * @Author: lxx
 * @Date: 2021/2/19 5:39 下午
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 配置Redis的两个操作组件:RedisTemplate & StringRedisTemplate
 * */
@Configuration
public class CommonConfig {

  @Autowired
  private RedisConnectionFactory redisConnectionFactory;

  @Bean
  public RedisTemplate<String, Object> redisTemplate(){
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    return redisTemplate;
  }

  @Bean
  public StringRedisTemplate stringRedisTemplate(){
    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
    stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
    return stringRedisTemplate;
  }
}

3、测试

package com.beauty.time;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
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.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Author: lxx
 * @Date: 2021/2/19 4:16 下午
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
  @Autowired
  private RedisTemplate redisTemplate;

  @Test
      public void testRedis(){

    //根据openid查询skey是否存在
    String skey_redis = (String) redisTemplate.opsForValue().get( "openid" );
    if(StringUtils.isNotBlank( skey_redis )){
      //存在 删除 skey 重新生成skey 将skey返回
      redisTemplate.delete( skey_redis );
    }
    //  缓存一份新的
    JSONObject sessionObj = new JSONObject(  );
    sessionObj.put( "openId","openid" );
    sessionObj.put( "sessionKey","sessionKey" );
    redisTemplate.opsForValue().set( "skey",sessionObj.toJSONString() );
    redisTemplate.opsForValue().set( "openid","skey" );

  }


}

单测跑通过后可以在客户端命令行下校验结果,可以看下缓存是否成功

127.0.0.1:6379> keys *
1) "skey"
2) "openid"
127.0.0.1:6379> get skey
"\xac\xed\x00\x05t\x00-{\"sessionKey\":\"sessionKey\",\"openId\":\"openid\"}"
127.0.0.1:6379> get openid
"\xac\xed\x00\x05t\x00\x04skey"

已存成功。

该文参考了如下文章:

redis安装

https://blog.csdn.net/realize_dream/article/details/106227622

springboot整合redis

https://www.cnblogs.com/huaiheng/p/12839989.html

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值