实际上,我有一个应用程序,它使用redis进行缓存,并将JWT令牌保存在Redis中。
我的目标是让一个redis用于缓存,另一个用于jwt令牌。
我不明白我怎么能做到这一点。如何用spring来使用特定的redis (在这里是"redis-cache")来进行缓存?实际上,我只放@EnableCaching,它运行正常
谢谢你的帮助
spring:
redis:
port: 7000
password: password123
host: 127.0.0.1
redis-cache: # New One
port: 7001
password: password123
host: 127.0.0.1我使用redisTemplate将jwt令牌保存在redis中
@Configuration
public class GenericBeanConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
Jackson2JsonRedisSerializer<String> jrs = new Jackson2JsonRedisSerializer<String>(String.class);
template.setKeySerializer(jrs);
template.setConnectionFactory(connectionFactory);
return template;
}
}...
@EnableCaching
public class ProjectsApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectsApplication .class, args);
}
}缓存某些端点
@Cacheable(value = "users-rbac")
public UserResponseDTO search(@PathVariable String email) {
return userService.search(email);
}发布于 2022-03-14 05:29:04
您也可以定义您的自定义CacheManager,您可以按以下方式定义您的缓存管理器。正如您所看到的,自定义它,现在主机、端口和密码都是固定的,但是您可以使用@Value从应用程序配置文件中读取它们。
@Bean
public CacheManager cacheManager() {
// create redis configuration
RedisStandaloneConfiguration configuration =
new RedisStandaloneConfiguration("127.0.0.1", 7001);
configuration.setPassword(RedisPassword.of("password"));
// create a connection factory, use other connection factory if you want
LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration);
factory.afterPropertiesSet();
RedisCacheWriter writer = RedisCacheWriter.nonLockingRedisCacheWriter(factory);
// define cache cnfiguration
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
// use Jackson serdes do not use JDK one
cacheConfiguration.serializeValuesWith(
SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
// create cache manager
return new RedisCacheManager(writer, cacheConfiguration);
}https://stackoverflow.com/questions/71445259
复制相似问题