question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redis Spring Cache usage in SDK

See original GitHub issue

Describe 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:open
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bdemerscommented, Aug 30, 2022

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.

0reactions
oussamalahloucommented, Sep 2, 2022

It’s clear, thank you @bdemers

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot Cache with Redis - Baeldung
In this short tutorial, we'll look at how to configure Redis as the data store for Spring Boot cache. Further reading: Spring Boot...
Read more >
Spring Boot Redis Cache - DigitalOcean
Redis Cache Limits ... Although Redis is very fast, it still has no limits on storing any amount of data on a 64-bit...
Read more >
Spring Caching with Redis - Medium
Redis keeps the data in memory and doesn't write data on the disk and hence it is very fast. However, Redis has the...
Read more >
Data Caching in a Spring Boot application with Redis
In this post, we will see how to use Redis to cache data retrieved from a MySQL database through a Spring Boot Application....
Read more >
Spring Data Redis
RedisTemplate uses a Java-based serializer for most of its operations. This means that any object written or read by the template is serialized ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found