RedisTemplate Spring Boot

bsxbgnwa  于 2022-12-17  发布在  Spring
关注(0)|答案(2)|浏览(110)

我要将令牌存储在Redis中

重新配置

@Configuration
@EnableCaching
public class RedisConfig {

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName("127.0.0.1");
        redisStandaloneConfiguration.setPort(6379);

        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate() {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

服务

final var st =AuthenticationSuccessDto
                .builder()
                .accessToken(jwtUtils.generateAccessToken(user))
                .refreshToken(jwtUtils.generateRefreshToken(user))
                .tokenType("Bearer")
                .user(user)
                .expiresIn(TimeUnit.SECONDS.toSeconds(60)).build();

        try {
            redisTemplate.opsForHash().put(KEY,"1",st.getAccessToken());
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception();
        }

        return st;
    }

抛出异常java.lang.NoSuchMethodError:'long redis.clients.jedis. jedis.hset(字节[],字节[],字节[])'

我需要将字符串转换为字节?

drkbr07n

drkbr07n1#

你也可以让Sping Boot 根据你的.yml或.properties文件自动生成到Redis的连接,你只需要在你的应用程序.yml中添加以下内容:

spring:
  redis:
    host: localhost
    port : '6379'
zyfwsgd6

zyfwsgd62#

当我将与Redis的连接从JedisConnectionFactory更改为LettuceConnectionFactory时,一切都很顺利

相关问题