java如何为从多个表连接的对象定义Ignite缓存
我正在使用Postgres,它被设置为Ignite的外部持久存储,我想知道有什么方法可以为数据分布在多个表中的对象定义缓存
例如,为了与此人和Car类及其下表合作,我提供了自己的CacheStore实现。然而,这种方法似乎非常冗长,因为我需要自己手动分配字段值。我还可以用其他方法来做这件事吗
人员类别:
public class PersonMO{
private int id;
private String name;
private String address;
private Set<Car> cars
public PersonMO() {};
public PersonMO(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "ID: "+id +", Name: "+name+" AD: " +address;
}
public void setCars(Set<Car> cars) {
this.cars = cars;
}
public Set<Car> getCars() {
return cars;
}
}
汽车等级
public class Car {
int id;
private String name;
public Car(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
缓存存储实现
public class PersonMOCacheStore implements CacheStore<Integer, PersonMO>{
@SpringResource(resourceName = "pgDataSource")
private DriverManagerDataSource pgDataSource;
@LoggerResource
private IgniteLogger log;
//public void loadCache(IgniteBiInClosure<Integer, PersonMO> clo, @Nullable Object... args)
@Override
public void loadCache(IgniteBiInClosure<Integer, PersonMO> clo, Object... args)
throws CacheLoaderException {
log.info(">> Loading cache from store...");
try(Connection conn = pgDataSource.getConnection()){
try(PreparedStatement st = conn.prepareStatement("select * from PERSON")){
try(ResultSet rs = st.executeQuery()){
while(rs.next()) {
PersonMO person = new PersonMO(rs.getInt(1),rs.getString(2), rs.getString(3));
person.setCars(getCarSet(conn, person.getId() ) );
clo.apply(person.getId(), person);
}
log.info(">> Finished Loading cache from store...");
}
}
}catch(SQLException e) {
throw new CacheLoaderException("Failed to load values from cache store.",e);
}
}
//implementation for IgniteCache.get
@Override
public PersonMO load(Integer key) throws CacheLoaderException {
log.info(">> Loading person from store...");
try (Connection conn = pgDataSource.getConnection()) {
try(PreparedStatement st = conn.prepareStatement("select * from PERSON where id = ?")){
st.setString(1, key.toString());
ResultSet rs = st.executeQuery();
if(rs.next()) {
PersonMO p= new PersonMO(rs.getInt(1),rs.getString(2), rs.getString(3));
//p.setCars( getCarSet(conn, p.getId() ) );
return p;
}else {
return null;
}
}
}catch(SQLException e) {
throw new CacheLoaderException("Failed to load values from cache store.",e);
}
}
private Set<Car> getCarSet(Connection conn, int personId) throws SQLException{
Set<Car> carSet = new HashSet<Car>();
PreparedStatement st = conn.prepareStatement("select * from CAR where id = "+ personId);
ResultSet rs = st.executeQuery();
while(rs.next()) {
carSet.add(new Car(rs.getInt(1),rs.getString(2) ));
}
return carSet;
}
//other methods needed left out for sake of simplicity
}
# 1 楼答案
可以使用CacheJdbcPojoStore填充缓存。然后可以通过SQL或键/值API访问数据
然而,它也同样冗长:)
https://www.gridgain.com/docs/latest/developers-guide/persistence/external-storage#cachejdbcpojostore