Redis Spring Cache usage in SDK
See original GitHub issueDescribe the feature request?
Hello,
In the documentation you can find this :
If your application is deployed on multiple JVMs (e.g. a distributed/clustered web app), you might not want to use the builders here and instead implement the CacheManager API directly to use your distributed/clustered cache technology of choice.
How are we supposed to do that when using Spring Boot AND Redis ? My point is that I run 3 differents Java apps, running on containers which can scale. I want those containers to share a same Redis Cache.
To do that I am guessing that I need to “extends” the DefaultCache implemented by Okta and override the methods : get / put / remove.
For exemple :
@Autowired
private RedisCacheManager cacheManager;
@Bean
protected Client oktaClient() {
return Clients.builder()
...
.setCacheManager(new CacheManager() {
@Override
public <K, V> Cache<K, V> getCache(String name) {
return new RedisSpringCache(name, cacheManager);
}
})
...
.build();
}
public class RedisSpringCache<K, V> extends DefaultCache<K, V> {
private org.springframework.cache.Cache springCache;
public RedisSpringCache(String name, RedisCacheManager redisCacheManager) {
super(name);
springCache = redisCacheManager.getCache(name);
}
@Override
public V get(K key) {
return (V) springCache.get(key).get();
}
@Override
public V put(K key, V value) {
springCache.put(key, value);
return value;
}
@Override
public V remove(K key) {
V value = (V) springCache.get(key).get();
springCache.evict(key);
return value;
}
}
Is there a simpler way to use the Redis Cache managed by Spring ?
For exemple :
@Autowired
private RedisCacheManager cacheManager;
@Bean
protected Client oktaClient() {
return CustomClients.builder()
...
.setCacheManager(cacheManager)
...
.build();
}
Any idea is welcomed, I am trying to find the best and easiest solution to maintain.
Thank you.
New or Affected Resource(s)
com.okta.sdk.cache.CacheManager; com.okta.sdk.client.Clients;
Provide a documentation link
Java Doc Sdk Cache :
Additional Information?
No
Issue Analytics
- State:
- Created a year ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
Hello @bcoquard
For Spring Boot support, see: https://github.com/okta/okta-sdk-java#inject-the-okta-java-sdk-in-spring
You won’t need to configure anything manually, just set the config properties and then inject the
Client
. Spring Cache will be used automatically (as long as an implementation is on your classpath).It looks like we should add a note in our READMEs though, we can keep this issue open to track that.
It’s clear, thank you @bdemers