apache / ignite

Apache Ignite
https://ignite.apache.org/
Apache License 2.0
4.83k stars 1.9k forks source link

Automated Cache initialisation #11451

Open PuszekSE opened 4 months ago

PuszekSE commented 4 months ago

I've been looking into using Ignite as an automated Caching layer on top of existing database table and I've just realised that it requires you to MANUALLY call LoadData after startup.

The setup I'm looking into is pure helm/k8s deployment, so all the cache configurations, query entities, connections, etc. already exist and are operational, but it seems like it's impossible to configure it to automatically scrape the data from the datasource.

From my understanding Ignite already operates on top of Spring, so it should be possible to include some basic cache initialisation capabilities.

Very simple (autogenerated) piece of code that I would consider, is something like this?... (Obviously it requires modifications inside related libraries, to actually properly pass the rest of configuration (In my case, mainly cacheStoreFactory configs, that already provide the information on how to actually load the cache)

@Configuration
@EnableScheduling
public class CustomCacheConfiguration<K, V> extends CacheConfiguration<K, V> {

    @IgniteInstanceResource
    private Ignite ignite;

    private String cronExpression;
    private boolean initializeOnCreate;

    public CustomCacheConfiguration(String name, String cronExpression, boolean initializeOnCreate) {
        super(name);
        this.cronExpression = cronExpression;
        this.initializeOnCreate = initializeOnCreate;
    }

    @PostConstruct
    public void postConstruct() {
        if (initializeOnCreate) {
            initializeCache();
        }
    }

    @Scheduled(cron = "#{@customCacheConfiguration.cronExpression}")
    public void scheduledInitializeCache() {
        initializeCache();
    }

    public void initializeCache() {
        IgniteCache<K, V> cache = ignite.getOrCreateCache(this);
        cache.loadCache(null);
    }
}

initializeCache is also a method that could be easily exposed as part of RestAPI.

I'm not sure whether it's desired feature, but I believe it's something that would greatly increase the usability of Ignite in somewhat basic scenarios, especially with current shift into full k8s-based environments.