我有三个具有继承和关系的模型,我想缓存对这个模型的查询。
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我如何用狗堆来缓存这样的查询?我可以将查询缓存到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)但我需要连接负荷来过滤类型。
发布于 2014-04-21 17:57:17
为了确保加载"man“表,可以将of_type()用于任何子类型模式。相反,我们可以使用with_polymorphic()连接到完全可选择的多态。有关此问题的详细信息,请参阅http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#creating-joins-to-specific-subtypes上的示例。只要您想要的数据在一个选择查询中显示出来,那么该数据就会在通过FromCache缓存的数据中。诚然,缓存配方目前并不包括一个系统,通过该系统,可以在事实发生后缓存附加连接继承属性的延迟加载。
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from examples.dogpile_caching.caching_query import query_callable, FromCache, RelationshipCache
from hashlib import md5
from dogpile.cache.region import make_region
Base = declarative_base()
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 SomethingElse(Person):
__tablename__ = 'somethingelse'
id = Column(Integer, ForeignKey('person.id'), primary_key=True)
age = Column(String(100), nullable=False)
__mapper_args__ = {'polymorphic_identity': 'somethingelse'}
class Config(Base):
__tablename__ = "config"
id = Column(Integer, primary_key=True)
person = Column(Integer, ForeignKey('person.id'))
address = Column(String)
person_ref = relationship(Person)
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
def md5_key_mangler(key):
"""Receive cache keys as long concatenated strings;
distill them into an md5 hash.
"""
return md5(key.encode('ascii')).hexdigest()
regions = {}
regions['default'] = make_region(
key_mangler=md5_key_mangler
).configure(
'dogpile.cache.memory_pickle',
)
Session = scoped_session(
sessionmaker(
bind=e,
query_cls=query_callable(regions)
)
)
sess = Session()
sess.add(Config(person_ref=SomethingElse(age='45', name='se1')))
sess.add(Config(person_ref=Man(age='30', name='man1')))
sess.commit()
all_types = with_polymorphic(Person, "*", aliased=True)
conf = sess.query(Config).options(joinedload(Config.person_ref.of_type(all_types)), FromCache("default")).first()
sess.commit()
sess.close()
print "_____NO MORE SQL!___________"
conf = sess.query(Config).options(joinedload(Config.person_ref.of_type(all_types)), FromCache("default")).first()
print conf.person_ref.agehttps://stackoverflow.com/questions/23102971
复制相似问题