springboot redis注解

在Spring Boot中使用Redis注解,可以方便地实现缓存功能,下面是详细的技术介绍:

1、引入依赖

springboot redis注解

在项目的pom.xml文件中添加spring-boot-starter-data-redis依赖:

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

2、配置Redis

在application.properties或application.yml文件中配置Redis连接信息:

application.properties
spring.redis.host=localhost
spring.redis.port=6379

或者

application.yml
spring:
  redis:
    host: localhost
    port: 6379

3、使用注解

在需要使用缓存的方法上添加@Cacheable注解,

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
    @Cacheable(value = "user", key = "id")
    public User getUserById(Long id) {
        // 查询数据库操作
        return user;
    }
}

在这个例子中,当调用getUserById方法时,会先检查缓存中是否存在对应的数据,如果存在则直接返回缓存数据,否则执行方法体中的代码并将结果存入缓存。

springboot redis注解

4、自定义缓存配置

如果需要自定义缓存配置,可以在配置类中创建RedisTemplate和CacheManager Bean:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching
public class RedisConfig {
    @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 GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
    @Bean
    public CacheManager cacheManager() {
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(3600) // 缓存过期时间,单位秒
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        return RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(redisCacheConfiguration)
                .transactionAware()
                .build();
    }
}

5、使用其他注解

除了@Cacheable注解外,还可以使用以下注解:

@CachePut:每次调用方法都会更新缓存,无论缓存是否存在。

@CacheEvict:删除缓存,可以指定删除某个缓存或所有缓存。

@Caching:组合多个缓存注解,实现更复杂的缓存逻辑。

springboot redis注解

相关问题与解答:

1、问题:如何在Spring Boot中使用Redis注解实现分布式锁

答案:可以使用@CachePut注解结合Redis的SETNX命令实现分布式锁,具体做法是在获取锁的方法上添加@CachePut注解,并设置key和value,当方法被调用时,会尝试将key-value存入缓存,如果成功则表示获取锁成功,否则表示锁已被其他线程持有,释放锁时,可以直接删除缓存中的key。

2、问题:如何自定义Redis序列化方式?

答案:可以在创建RedisTemplate时设置自定义的序列化方式,例如使用StringRedisSerializer作为key的序列化方式,使用GenericJackson2JsonRedisSerializer作为value的序列化方式,具体代码如下:

redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/287723.html

(0)
K-seoK-seoSEO优化员
上一篇 2024年2月4日 23:22
下一篇 2024年2月4日 23:26

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入