SpringBoot ��������浽redis�ķ�ʽ

��Spring Boot��Ŀ�У������ݿ��ѯ������浽Redis��һ����������������������ݷ����ٶȺͼ������ݿ⸺�ء������Ǽ���ʵ����һ����ij���������

1. ʹ��Spring Data Redis�ֶ��洢

���ַ����漰��ֱ��ʹ��RedisTemplate���ֶ��������档��DataCacheService���У�ͨ��@Autowiredע��RedisTemplate�����ݿ���ʵ�YourRepository����@PostConstructע��ķ����У���Ŀ������ִ�����ݿ��ѯ������������浽Redis�С�

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Service
public class DataCacheService {

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@Autowired
private YourRepository yourRepository;

@PostConstruct
public void init() {
List<YourEntity> data = yourRepository.findAll();
redisTemplate.opsForValue().set("cacheKey", data);
redisTemplate.expire("cacheKey", 1, TimeUnit.HOURS); // ���ù���ʱ��
}
}

�ŵ���ֱ�ӿ��ƻ�������ݺ��������ڡ�
ȱ������Ҫ�ֶ���������ĸ��º�ʧЧ��

2. ʹ��@Cacheableע���Զ�����

Spring Cache�ṩ��һ������ʽ�Ļ������@Cacheableע������Զ����淽���ķ���ֵ�����ȣ���Ҫ����һ��CacheManager������ʹ��Redis��Ϊ����洢��Ȼ������Ҫ����ķ�����ʹ��@Cacheableע�⡣

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.builder(factory).cacheDefaults(config).build();
}
}

@Service
public class YourService {

@Autowired
private YourRepository yourRepository;

@Cacheable(value = "yourCache", key = "'allData'")
public List<YourEntity> getAllData() {
return yourRepository.findAll();
}
}

�ŵ������˻��������ֻ��ע�⼴�ɡ�
ȱ�������ܲ����ֶ��������ر����ڸ��ӵĻ�������ϡ�

3. ʹ��ApplicationRunner��CommandLineRunner�ӿ�

�������ӿ�������Spring BootӦ������ʱ���д��롣���Ƿdz��ʺ����ڳ�ʼ�����ݻ���ػ��档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component
public class RedisDataLoader implements ApplicationRunner {

@Autowired
private YourRepository yourRepository;

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@Override
public void run(ApplicationArguments args) throws Exception {
List<YourEntity> data = yourRepository.findAll();
redisTemplate.opsForValue().set("initialData", data);
}
}

�ŵ����ʺ���Ӧ������ʱִ��һ��������
ȱ����������������ʱ�����ݼ��أ����ʺ϶�̬������¡�

4. ʹ��@EventListener����Ӧ�������¼�

���ַ���ͨ������ApplicationReadyEvent�¼�����Ӧ����ȫ������ִ�д��롣��Ȼ���ַ�����ApplicationRunner��CommandLineRunner���ƣ����ṩ�˸��������ԣ��ر�������Ҫ��ϸ���ȵĿ���ʱ��

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Service
public class StartupDataLoader {

@Autowired
private YourRepository yourRepository;

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@EventListener(ApplicationReadyEvent.class)
public void loadDataOnStartup() {
List<YourEntity> data = yourRepository.findAll();
redisTemplate.opsForValue().set("startupData", data);
}
}

�ŵ����ṩ����Ӧ����ȫ������ִ�д��������ԡ�
ȱ������ApplicationRunner��ȣ����������΢����һЩ��

�ܽ�

ѡ�����ַ���ȡ���ھ���������Ӧ�ó����������Ҫ�򵥵Ļ��������@Cacheableע���������õ�ѡ�������Ҫ�����Ŀ��ƻ���Ҫ��Ӧ������ʱִ���ض��ij�ʼ������ApplicationRunner��CommandLineRunner��@EventListener���ܸ��ʺϡ��ֶ�ʹ��RedisTemplate�ṩ����������ԣ���Ҳ��Ҫ���Ĵ����ά��������

 wechat
天生我才必有用