SpringBoot集成Redis,做登录缓存

    一般用户从浏览器登录系统, 用户信息保存在 Session 中。

    设想场景:一个大应用包含多个子应用,这些子应用需要做相同的身份验证,如果每次登录子应用都需要做身份验证,那么在该大应用切换子应用就需要耗费很多时间,那么就需要在整个应用中考虑共享 Session 来提高效率,避免频繁查询数据库,提高效率,减轻数据库(具体例子如微信企业号中的应用间共享 Session)

      下面简单演示 SpringBoot 操作 Redis 数据库,在此只演示 SpringBoot 中 Redis 的使用, 登录缓存例子请查看源项目:

      百度网盘: https://pan.baidu.com/s/1_d9U1mS5NvGdE0Vpu8YMWg 提取码: i4n9 

      CSDN:https://download.csdn.net/download/zcf980/10975590

      (抱歉!!下载积分无法自定义,喜欢的可以支持)

      1. 首先引入相关依赖:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

      2. 在 application.yml 中添加 redis 的配置信息     

redis:
    database: 0  # Redis数据库索引(默认为0)
    host: localhost
    port: 6379
    pool:
      max-active: 8  # 连接池最大连接数(使用负值表示没有限制)
      max-wait: -1  # 连接池最大阻塞等待时间(使用负值表示没有限制)
      max-idle: 8  # 连接池中的最大空闲连接
      min-idle: 0  # 连接池中的最小空闲连接
    timeout: 0

      配置了以上信息后,Springboot 将自动维护两个 RedisTemplate 的 Bean(RedisTemplate<String, String> 和 RedisTemplate<Object, Object>), 在需要操作Redis的类中注入RedisTemplate 即可操作Redis。 我们可以在测试类中测试这两个Bean

      3. 测试 RedisTemplate<String, String> 和 RedisTemplate<Object, Object>

package com.example.springbootredis.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Author zcf
 * @Create 2019/2/6 14:58
 * @Desc
 */

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureWebMvc
@WebAppConfiguration
public class RedisTest {

//    @Autowired
//    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Test
    public void testSet() {

       // this.redisTemplate.opsForValue().set("study", "java");
       // System.out.println("study" + this.redisTemplate.opsForValue().get("study"));

        // 设置字符串序列化器,使 key 在 Redis 中以字符串形式显示,否则会出现十六进制代码,不方便查看
        this.redisTemplate.setKeySerializer(new StringRedisSerializer());

        List<String> list = Arrays.asList(new String[]{"zcf1", "zcf2", "zcf3", "zcf4"});
        this.redisTemplate.opsForValue().set("test-list", list);

        List<String> result = (List<String>) this.redisTemplate.opsForValue().get("test-list");
        System.out.println(result);

    }

}

   运行测试代码:   

 

登录例子请下载源码!!!!!!!

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值