2017-06-17 21 views
0

目前我們正在使用番石榴來緩存幾個db實體。我正在評估分發Apache的點火取代番石榴。使用CacheLoader時,番石榴具有語義get-if-absent。如何通過點燃來實現相同的功能。Apache Ignite緩存未命中處理程序

回答

0

通過這篇文章https://dzone.com/articles/apache-ignite-how-to-read-data-from-persistent-sto得到了答案。如果有高速緩存未命中,我們可以從數據庫中通過緩存存儲如下

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. 
… 

}

把它拿來