Reputation: 195
I have an API, which takes 2 minutes to load the result from the LDAP. So, to solve this ,I have used @EnableCaching to cache the result of this API. But the problem is, after every deployment of this application, I have to hit this API first, so that result of this api could be cached, which is not a good practice. Is there a way , by which this api could be hit before application gets started ,like during the deployment. or there is any other way , so that I don't have to hit this api explicitly , after every deployment ?
Upvotes: 1
Views: 85
Reputation: 455
Details about your LDAP server needs more clarification on updates.
However at this moment simply register a CommandLineRunner
, @Autowire
your service and initialise your Cache Key.
@Component
public class AppRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(AppRunner.class);
private final LdapRepository ldapRepository;
public AppRunner(LdapRepository ldapRepository) {
this.ldapRepository = ldapRepository;
}
@Override
public void run(String... args) throws Exception {
logger.info(".... Fetching Users");
this.ldapRepository.getAllUsers();
}
}
@Component
public class SimpleLdapRepository implements LdapRepository {
@Override
@Cacheable("users")
public List<User> getAllUsers() {
return ... data from ldap call
}
}
Upvotes: 1