0

I am trying to implement an in-memory cache in a Spring Boot application using Java 8. The cache should store a list of String values, and it should automatically load with values at application startup.

Here are the key requirements:

  1. In-memory storage: The cache should reside in memory.
  2. Stores list of strings: The cache will store a collection of String values.
  3. Automatic loading at startup: The cache should be populated with initial data when the application starts (e.g., loading data from a file or database).
  4. Java 8-specific approaches: I'm curious if there are any features or approaches in Java 8 that could make this implementation more efficient or cleaner.

I have considered using Spring's @Cacheable annotation, but I am not sure if that's the best approach, given that I want the cache to load automatically during startup.

Could you suggest an approach to implement this cache efficiently using Spring Boot and Java 8?

What I’m looking for:

  • Efficient in-memory cache with automatic loading.
  • Java 8-specific tools or optimizations (if available).
  • A code example of how the cache can be implemented, initialized at startup, and accessed during runtime.
1
  • You can implement a method to load data from a file or database and annotate it with @Cacheable. To trigger this method during the startup phase, you can use various approaches such as listening for the ApplicationStartedEvent with @EventListener, implementing the CommandLineRunner interface, or using the @PostConstruct annotation. For more details, please refer to the relevant topic here. Commented 17 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.