以下示例中,学校模型具有唯一字段“key” 一旦我打电话给School.get(1), 当我再次打电话给School.get(1)时。 没有查询,这很好。 但是当打电话时学校.get_by_key('gxu') 查询电话。
我想要的是,因为“密钥”是唯一的,所以SQLALchemy可以聪明不再查询吗?
In [3]: school = School.get(1)
INFO-2013-06-14 18:08:17,903:BEGIN (implicit)
INFO-2013-06-14 18:08:17,904:SELECT school_info._id AS school_info__id, school_info."key" AS school_info_key, school_info.name AS school_info_name
FROM school_info
WHERE school_info._id = ?
INFO-2013-06-14 18:08:17,905:(1,)
DEBUG-2013-06-14 18:08:17,906:Col ('school_info__id', 'school_info_key', 'school_info_name')
DEBUG-2013-06-14 18:08:17,906:Row (1, u'gxu', u'\u5e7f\u897f\u5927\u5b66')
In [4]: gxu_school = School.get_by_key('gxu')
INFO-2013-06-14 18:08:32,042:SELECT school_info._id AS school_info__id, school_info."key" AS school_info_key, school_info.name AS school_info_name
FROM school_info
WHERE school_info."key" = ?
LIMIT ? OFFSET ?
INFO-2013-06-14 18:08:32,045:(u'gxu', 1, 0)
DEBUG-2013-06-14 18:08:32,046:Col ('school_info__id', 'school_info_key', 'school_info_name')
DEBUG-2013-06-14 18:08:32,047:Row (1, u'gxu', u'\u5e7f\u897f\u5927\u5b66')
In [5]:
答案 0 :(得分:0)
首先,我认为get_by_key()是0.3或者某种古老的方法吗?时间升级。
接下来,对于任意唯一字段的“缓存”,您可以尝试一个配方:UniqueObject。
或者,如果您感觉更有野心,可以使用真实的caching setup。