SpringBoot(九):redis缓存数据和session

栏目: Java · 发布时间: 6年前

内容简介:本作品采用本文作者:低调小熊猫文章链接:

本作品采用 知识共享署名 4.0 国际许可协议 进行许可。

本文作者:低调小熊猫

文章链接: https://aodeng.cc/archives/springbootjiu

转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032

配置

pom.xml

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

application.yml

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring:
  redis:
      database: 0
  # Redis服务器地址
      host: localhost
# Redis服务器连接端口
      port: 6379
# Redis服务器连接密码(默认为空)
      password:
# 连接超时时间(毫秒)
      #timeout: 0
  # 连接池最大连接数(使用负值表示没有限制)
  pool:
    max-active: 8
  # 连接池最大阻塞等待时间(使用负值表示没有限制)
    max-wait: -1
  # 连接池中的最大空闲连接
    max-idle: 8
  # 连接池中的最小空闲连接
    min-idle: 0

RedisConfig.java

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object object : params) {
                    sb.append(object.toString());
                }
                return sb.toString();
            }
        };
    }

    /***
     * springboot 2.0
     * @param connectionFactory
     * @return
     */
    @Bean
    CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        //user信息缓存配置
        RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(30)).disableCachingNullValues().prefixKeysWith("role");
        //product信息缓存配置
        RedisCacheConfiguration productCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)).disableCachingNullValues().prefixKeysWith("test");
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        redisCacheConfigurationMap.put("role", userCacheConfiguration);
        redisCacheConfigurationMap.put("test", productCacheConfiguration);
        //初始化一个RedisCacheWriter
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);


        //设置CacheManager的值序列化方式为JdkSerializationRedisSerializer,但其实RedisCacheConfiguration默认就是使用StringRedisSerializer序列化key,JdkSerializationRedisSerializer序列化value,所以以下注释代码为默认实现
        //ClassLoader loader = this.getClass().getClassLoader();
        //JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(loader);
        //RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jdkSerializer);
        //RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);


        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
        //设置默认超过期时间是30秒
        defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
        //初始化RedisCacheManager
        RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
        return cacheManager;
    }
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

SessionConfig.java

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1)
public class SessionConfig {
}

使用缓存

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedisTest {
    private static final Logger log= LoggerFactory.getLogger(TestRedisTest.class);
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @Test//StringRedisTemplate使用的是 StringRedisSerializer
    public void test1() throws Exception{
        stringRedisTemplate.opsForValue().set("name","admin");
        Assert.assertEquals("admin",stringRedisTemplate.opsForValue().get("name"));
        log.info("[redis测试]-[{}]",stringRedisTemplate.opsForValue().get("name"));
    }
    @Test//RedisTemplate使用的是 JdkSerializationRedisSerializer
    public void test2() throws Exception{
        SysRole sysRole=new SysRole();
        sysRole.setRoleId(1);
        sysRole.setRole("1001");
        sysRole.setRole("管理员");
        sysRole.setDescription("这是一个测试管理员");
        ValueOperations<String,SysRole> valueOperations=redisTemplate.opsForValue();
        valueOperations.set("sysRole1",sysRole);
        valueOperations.set("sysRole2",sysRole,2, TimeUnit.SECONDS);
        Thread.sleep(1000);
        log.info("[sysRole2]-[{}]",valueOperations.get("sysRole2"));
        boolean exists=redisTemplate.hasKey("sysRole2");
        if(exists){
            System.out.println("true");
        }else {
            System.out.println("false");
        }
        Assert.assertEquals("管理员",valueOperations.get("sysRole1").getRole());
    }
}

session共享demo

@RestController

public class TestRedis {

@RequestMapping("/test")
String test(HttpSession httpSession){
    UUID uuid=(UUID) httpSession.getAttribute("uuid");
    if(null==uuid){
        uuid=UUID.randomUUID();
    }
    httpSession.setAttribute("uuid",uuid);
    return httpSession.getId();
}

}

redis客户端查看session

keys '*sessions*'

以上所述就是小编给大家介绍的《SpringBoot(九):redis缓存数据和session》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Refactoring

Refactoring

Martin Fowler、Kent Beck、John Brant、William Opdyke、Don Roberts / Addison-Wesley Professional / 1999-7-8 / USD 64.99

Refactoring is about improving the design of existing code. It is the process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its int......一起来看看 《Refactoring》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具