简体   繁体   中英

Ignite loadCache() and write behind

We're using Ignite, and for this particular cache we want to use write behind to insert new rows in an RDBMS when items are added to the cache.

The wrinkle is, we want to initially load the cache with existing rows.

At the moment Ignite attempts to write the items loaded in cache.loadCache(), which is obviously not the desired behavior. I'd like to somehow indicate that these items don't need to be persisted.

My searches didn't turn up anything useful, please advise if you can. It's much appreciated!

IgniteCache#loadCache should ignore writeThrough, in other words, records loaded via loadCache method should not be inserted to RDBMS.

The second option is to manually load the data using a cache/datastreamer instance obtained via IgniteCache#witkSkipStore() or IgniteDataStreamer#skipStore.

public class CacheStoreExample {
public static void main(String[] args) {
    Ignite ignite = Ignition.start(
        new IgniteConfiguration()
            .setCacheConfiguration(new CacheConfiguration("cache")
                .setWriteThrough(true)
                .setWriteBehindEnabled(true)
                .setCacheStoreFactory(new Factory<CacheStore>() {
                    @Override public CacheStore create() {
                        return new CacheStoreAdapter() {
                            @Override public void loadCache(IgniteBiInClosure clo, Object... args) {
                                System.out.println("Loading cache with single a single key/value:");
                                clo.apply(1,1);
                            }
                            @Override public Object load(Object key) throws CacheLoaderException {return null; }
                            @Override public void write(Cache.Entry entry) throws CacheWriterException {
                                System.out.println("Writing entry: "+entry);
                            }
                            @Override public void delete(Object key) throws CacheWriterException {}
                        };
                    }
                })
    ));

    IgniteCache<Object, Object> cache = ignite.cache("cache");

    cache.loadCache((k, v) -> true);
    System.out.println("The first record:"+cache.get(1));

    System.out.println("Put operation skipping store");
    cache.withSkipStore().put(2,2);
    System.out.println("The second records:"+cache.get(2));

    System.out.println("Put operation with write trough");
    cache.put(3,3);
    System.out.println("The third record:"+cache.get(3));
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM