我有三个具有继承和关系的模型,我想缓存对这些模型的查询。
class Person(Base):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
type = Column(String(50))
__mapper_args__ = {
'polymorphic_identity': 'object',
'polymorphic_on': type
}
class Man(Person):
__tablename__ = 'man'
id = Column(Integer, ForeignKey('person.id'), primary_key=True)
age = Column(String(100), nullable=False)
__mapper_args__ = {'polymorphic_identity': 'man'}
class Config(Base):
__tablename__ = "config"
id = Column(Integer, primary_key=True)
person = Column(Integer, ForeignKey('person.id'))
address = Column(String)
person_ref = relationship(Person)
从Personal继承了很多其他模型。例如,我需要通过Config关系访问Man属性。通常我会这样做:
config = session.query(Config).join(Config.person_ref).filter(Person.type == 'man').first()
print config.person_ref.age
如何使用 dogpile 缓存这样的查询?我可以将查询缓存到Config,但我不能将查询缓存到Man的属性,每次都会发出 SQL。我尝试使用 with_polymorphic,但它只能在没有连接负载的情况下使用。(不明白为什么)
config = session.query(Config).options(FromCache("default")).first()
people = session.query(Person).options(FromCache("default")).with_polymorphic('*').get(config.person)
但我需要joinload 来过滤类型。