我有一个
EmployeeDaoImpl
具有
saveEmployee
方法,该方法使用
putIfAbsent
方法
hashOperations
存储
JSON
Redis中的字符串。
@Service
public class EmployeeDaoImpl implements IEmployeeDao {
private static final String MAPPING_KEY = "EMPLOYEES_REG_TEMP_RECORDS";
private final ObjectMapper objectMapper;
private final StringRedisTemplate stringRedisTemplate;
@Resource(name = "stringRedisTemplate") // 'stringRedisTemplate' is defined as a Bean in AppConfig.java
private HashOperations<String, String, String> hashOperations;
public EmployeeDaoImpl(ObjectMapper objectMapper, StringRedisTemplate stringRedisTemplate) {
this.objectMapper = objectMapper;
this.stringRedisTemplate = stringRedisTemplate;
}
@Override
public void saveEmployee(Employee emp) throws JsonProcessingException {
String json = objectMapper.writeValueAsString(emp);
hashOperations.putIfAbsent(MAPPING_KEY, emp.getRequestRefId(), json);
}
}
此屏幕截图是如何在Redis中保存员工记录的
我可以通过这种方式使密钥过期
stringRedisTemplate.expire(key, 30, TimeUnit.SECONDS)
但现在我希望能够为
员工记录
哈希密钥而不使密钥过期