我正在尝试使用Spring集成Redis / Jedis,但在设置键的过期时间后遇到了困难。
请问有人可以帮忙吗?
public class SessionCacheRepositoryImpl implements SessionCacheRepository {
private static final String KEY = "Session";
private RedisTemplate<String, Object> redisTemplate;
private HashOperations hashOperations;
@Autowired
public SessionCacheRepositoryImpl(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@PostConstruct
private void init() {
hashOperations = redisTemplate.opsForHash();
redisTemplate.expire(KEY, 30, TimeUnit.SECONDS);
}
public void saveSession(final Session session) {
hashOperations.put(KEY, session.getSessionID(), session);
}
}
这是我的配置类
private RedisTemplate<String, Object> template;
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
jedisConFactory.setHostName("localhost");
jedisConFactory.setPort(36919);
return jedisConFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
template = new RedisTemplate<String, Object>();
template.setConnectionFactory(jedisConnectionFactory());
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;
}
hashOperations.put(session.getSessionID(), session.getSessionID(), session); redisTemplate.expire(session.getSessionID(), 30, TimeUnit.MINUTES);
有没有办法我可以只使用 sessionID 作为键并保存我的对象? - Happy