微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

springboot整合redis的方法是什么

这篇文章主要讲解了“springboot整合redis方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springboot整合redis方法是什么”吧!

概述

springboot通常整合redis,采用的是Redistemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring支持的注解进行访问缓存 。

》准备工作

pom.xml文件

      <dependency>

            <groupId>redis.clients</groupId>

            <artifactId>jedis</artifactId>

            <version>2.7.3</version>

     </dependency>

     <dependency>

            <groupId>org.springframework.data</groupId>

            <artifactId>spring-data-redis</artifactId>

            <version>1.7.2.RELEASE</version>

     </dependency>

     <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-redis</artifactId>

            <version>RELEASE</version>

     </dependency>

application.properties配置文件

# REdis (RedisProperties)

# Redis数据库索引(认为0)

spring.redis.database=0

# Redis服务器地址

spring.redis.host=127.0.0.1

# Redis服务器连接端口

spring.redis.port=6379

# 连接池最大连接数(使用负值表示没有限制)

spring.redis.pool.max-active=8

# 连接池最大阻塞等待时间(使用负值表示没有限制)

spring.redis.pool.max-wait=-1

# 连接池中的最大空闲连接

spring.redis.pool.max-idle=8

# 连接池中的最小空闲连接

spring.redis.pool.min-idle=0

# 连接超时时间(毫秒)

spring.redis.timeout=0

Redis配置类

/**

 * @author hulonghai

 * redis配置类

 */

@Configuration

@EnableCaching

public class CacheConfig extends CachingConfigurerSupport{

    @SuppressWarnings("rawtypes")

    @Bean

    public CacheManager cacheManager(Redistemplate redistemplate) {

        RedisCacheManager rcm = new RedisCacheManager(redistemplate);

        // 多个缓存的名称,目前只定义了一个

        rcm.setCacheNames(Arrays.asList("thisredis"));

        //设置缓存过期时间(秒)

        rcm.setDefaultExpiration(600);

        return rcm;

    }

    @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;

    }

}

可以看出,我们这里主要配置了两个东西,cacheManager方法配置了一个缓存名称,它的名字叫做thisredis,当我们要在方法注解里面使用到它的时候,就要根据名称进行区分不同缓存。同时设置了缓

存的过期时间。redistemplate则是比较常见的,我们设置了Redistemplate,因此在代码里面,我们也可以通过@Autowired注入Redistemplate来操作redis.

使用

接下来就是如何使用注解啦,这一步反而是最简单的。其实只用到了两个注解,@Cacheable和@Cacheevict。第一个注解代表从缓存中查询指定的key,如果有,从缓存中取,不再执行方法。如果没有则执

方法,并且将方法的返回值和指定的key关联起来,放入到缓存中。而@Cacheevict则是从缓存中清除指定的key对应的数据。使用的代码如下:

@Cacheable(value="thisredis", key="'users_'+#id")

    public User findUser(Integer id) {

        User user = new User();

        user.setUsername("hlhdidi");

        user.setPassword("123");

        user.setUid(id.longValue());

        System.out.println("log4j2坏啦?");

        logger.info("输入user,用户名:{},密码:{}",user.getUsername(),user.getpassword());

        return user;

    }

    @Cacheevict(value="thisredis", key="'users_'+#id",condition="#id!=1")

    public void delUser(Integer id) {

        // 删除user

        System.out.println("user删除");

    }

可以看出,我们用@Cacheable的value属性指定具体缓存,并通过key将其放入缓存中。这里key非常灵活,支持spring的el表达式,可以通过方法参数产生可变的key(见findUser方法),也可以通过其指定在

什么情况下,使用/不使用缓存(见delUser方法)。

感谢各位的阅读,以上就是“springboot整合redis方法是什么”的内容了,经过本文的学习后,相信大家对springboot整合redis方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程之家,小编将为大家推送更多相关知识点的文章,欢迎关注!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐