0
votes

Currently we are using guava for caching few db entities. I am evaluating distribute apache ignite to replace guava. In guava have semantics get-if-absent using CacheLoader. How can achieve same functionality with ignite.

1

1 Answers

0
votes

Got the answer through this great article https://dzone.com/articles/apache-ignite-how-to-read-data-from-persistent-sto . If there is a cache miss we can fetch it from db through cache store as below

public class PersonStore implements CacheStore<Long, Person> {

@SpringResource(resourceName = "dataSource")
private DataSource dataSource;

// This method is called whenever IgniteCache.loadCache() method is called.
@Override
public void loadCache(IgniteBiInClosure<Long, Person> clo, @Nullable Object... objects) throws CacheLoaderException {
    System.out.println(">> Loading cache from store...");

    try (Connection conn = dataSource.getConnection()) {
        try (PreparedStatement st = conn.prepareStatement("select * from PERSON")) {
            try (ResultSet rs = st.executeQuery()) {
                while (rs.next()) {
                    Person person = new Person(rs.getLong(1), rs.getLong(2), rs.getString(3), rs.getInt(4));

                    clo.apply(person.getId(), person);
                }
            }
        }
    }
    catch (SQLException e) {
        throw new CacheLoaderException("Failed to load values from cache store.", e);
    }
}

// This method is called whenever IgniteCache.get() method is called.
@Override
public Person load(Long key) throws CacheLoaderException {
    System.out.println(">> Loading person from store...");

    try (Connection conn = dataSource.getConnection()) {
        try (PreparedStatement st = conn.prepareStatement("select * from PERSON where id = ?")) {
            st.setString(1, key.toString());

            ResultSet rs = st.executeQuery();

            return rs.next() ? new Person(rs.getLong(1), rs.getLong(2), rs.getString(3), rs.getInt(4)) : null;
        }
    }
    catch (SQLException e) {
        throw new CacheLoaderException("Failed to load values from cache store.", e);
    }
}

// Other CacheStore method implementations.
…

}