0

I'm working on micorservice using springboot . I have three questions here . Answers to any/all are much appreciated .Thanks in advance

Background: We need to read some key from vault during application startup and save it in variable for later use (to avoid hits on vault) . There will be TTL for this value so application should refresh and take whenever new value configured in vault.

Q1 : How to load and ensure values are loaded only once(i.e vault hit only once) Q2 :How to get the new values whenever there is a change Q3 : How to test locally.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
MannU
  • 13
  • 3
  • Have you tried using [Spring Vault](https://github.com/spring-projects/spring-vault) with [`@VaultPropertySource(…)`](https://docs.spring.io/spring-vault/docs/current/reference/html/#_vaultpropertysource)? – mp911de Mar 22 '18 at 15:08

1 Answers1

0

Use guava cache to store values (assuming they are strings, but you can change it to any type) like this:

LoadingCache<String, String> vaultData = CacheBuilder.newBuilder()
   .expireAfterAccess(10, TimeUnit.MINUTES)
   .build(
       new CacheLoader<String, String>() {
         public String load(String key) throws AnyException {
           return actuallyLoadFromVault(String);
         }
       });

This way when your code will read some key from vaultData for the first time it will loaded using actuallLoadFromVault (which you need to write of cause) and after that any new access to that key via vaultData will hit the cached value that is stored in memory.

With proper configuration after 10 minutes the value will be wiped from the cache (please read https://github.com/google/guava/wiki/CachesExplained#when-does-cleanup-happen and How does Guava expire entries in its CacheBuilder? to configure that correctly).

You might need to set max cache size to limit the memory consumption. See documentation for details.

  • Thanks, I will try this out . So from this our application periodically (for every ten mins) hits vault , No matter whether value in the vault changed or not is it ? – MannU Mar 19 '18 at 21:00