R Ram
R Ram

Reputation: 195

API takes 2 minutes to load data. Want to use cache

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

Answers (2)

rohitpal
rohitpal

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

Kayaman
Kayaman

Reputation: 73558

Use a 3rd party cache (such as Redis) that runs on its own server and isn't tied to your application. This way the cache will stay hot between redeploys, and you won't have this problem.

Upvotes: 0

Related Questions