Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add distributed lazy-loading cache #594

Open
pditommaso opened this issue Aug 6, 2024 · 0 comments
Open

Add distributed lazy-loading cache #594

pditommaso opened this issue Aug 6, 2024 · 0 comments

Comments

@pditommaso
Copy link
Contributor

Wave uses a lazy-loading cache in several components. See #589.

In some case it's needed to share the content of the cache across backend replicas. This has been recently implemented by #588 for the RegistryAuthServiceImpl. However it requires some custom code.

The goal of this issue is to implement a lazy-loading cache that implement the logic to persist item into a shared store such Redis.

A draft implementation could be the following

 import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import redis.clients.jedis.Jedis;

import java.util.concurrent.TimeUnit;

public class HybridCache<K, V> {

    private final Cache<K, V> caffeineCache;
    private final Jedis jedis;

    public HybridCache() {
        this.caffeineCache = Caffeine.newBuilder()
                .expireAfterWrite(10, TimeUnit.MINUTES)
                .maximumSize(100)
                .build();

        this.jedis = new Jedis("localhost", 6379);
    }

    public void put(K key, V value) {
        // Put in Caffeine
        caffeineCache.put(key, value);

        // Put in Redis
        jedis.set(key.toString(), value.toString());
    }

    public V get(K key, Class<V> clazz) {
        // Try to get from Caffeine
        V value = caffeineCache.getIfPresent(key);
        if (value != null) {
            return value;
        }

        // If not in Caffeine, try to get from Redis
        String redisValue = jedis.get(key.toString());
        if (redisValue != null) {
            // Optionally, convert the Redis value from String to the desired type
            // For simplicity, assuming V is String. Modify as needed.
            value = clazz.cast(redisValue);
            // Put in Caffeine for faster subsequent access
            caffeineCache.put(key, value);
        }
        return value;
    }

    public void invalidate(K key) {
        // Invalidate in Caffeine
        caffeineCache.invalidate(key);

        // Remove from Redis
        jedis.del(key.toString());
    }

}

Requirement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant