若依框架(RuoYi)是一个基于Spring Boot和Spring Cloud的快速开发平台,其功能强大且易于扩展。在实际项目中,为了提升系统性能和用户体验,通常会引入缓存机制。Redis因其高性能、丰富的数据结构支持以及持久化能力,成为缓存的首选方案。本文将详细介绍如何在若依框架中整合Redis缓存,并通过实战案例展示具体实现步骤。
在Web应用中,频繁访问数据库可能会导致性能瓶颈。Redis作为内存级别的键值存储系统,可以显著减少数据库的压力。若依框架中整合Redis缓存后,可以实现以下目标:
在开始整合之前,请确保以下环境已搭建完成:
在application.yml
文件中添加Redis的连接配置:
spring:
redis:
host: 127.0.0.1 # Redis服务器地址
port: 6379 # Redis端口号
password: # 如果有密码则填写
timeout: 5000 # 超时时间(毫秒)
lettuce:
pool:
max-active: 8 # 最大连接数
max-wait: -1 # 连接池最大等待时间
max-idle: 5 # 最大空闲连接数
min-idle: 0 # 最小空闲连接数
在主类或配置类上添加@EnableCaching
注解以启用Spring Cache功能:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class RuoYiApplication {
public static void main(String[] args) {
SpringApplication.run(RuoYiApplication.class, args);
}
}
创建一个配置类,用于设置Redis缓存的序列化方式:
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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 设置键的序列化方式为String
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
// 设置值的序列化方式为JSON
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
Spring Cache提供了多种注解来简化缓存操作,以下是常用的几个注解及其作用:
@Cacheable
:缓存方法返回值。@CachePut
:更新缓存。@CacheEvict
:清除缓存。以下是一个使用@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) {
System.out.println("从数据库中获取用户信息...");
// 模拟从数据库查询
return new User(id, "张三");
}
}
在上述代码中,当调用getUserById
方法时,Spring会首先检查Redis缓存中是否存在对应的键。如果存在,则直接返回缓存中的数据;否则,执行方法并将结果存入缓存。
在实际项目中,缓存的时效性非常重要。可以通过以下几种方式设置缓存失效策略:
@CacheEvict
注解。expire
命令设置缓存的有效期。import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@CacheEvict(value = "user", key = "#id")
public void deleteUserById(Long id) {
System.out.println("删除用户信息并清除缓存...");
}
}
在RedisConfig
中添加全局过期时间配置:
@Bean
public RedisCacheConfiguration cacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30)) // 设置缓存过期时间为30分钟
.disableCachingNullValues(); // 禁止缓存null值
}
在高并发场景下,为了避免多线程或分布式系统中的竞争问题,可以使用Redis实现分布式锁。以下是一个简单的分布式锁实现:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class DistributedLockService {
private static final String LOCK_PREFIX = "lock:";
@Autowired
private StringRedisTemplate redisTemplate;
public boolean tryLock(String lockKey, long expireTime, TimeUnit timeUnit) {
String key = LOCK_PREFIX + lockKey;
Boolean success = redisTemplate.opsForValue().setIfAbsent(key, "locked", expireTime, timeUnit);
return success != null && success;
}
public void unlock(String lockKey) {
String key = LOCK_PREFIX + lockKey;
redisTemplate.delete(key);
}
}
mget
和mset
)来减少网络延迟。redis-cli --stat
或第三方工具(如Redis Desktop Manager)进行实时监控。graph TD A[请求] --> B{缓存命中?} B --是--> C[返回缓存数据] B --否--> D[查询数据库] D --> E[存入缓存] E --> F[返回数据]