def sync_get_by_cid(cls, cid, read_preference=None, session=None, **kwargs): """ 依据主键_id获取Document :param cid: 记录主键(cid) :param read_preference: :param session: :param kwargs :return: """ result, clazz = None, cls() if clazz.__cached: result = clazz.result_2_obj(CacheEngine.get(cid)) if not result and clazz: collection = clazz.get_sync_collection(read_preference) if collection: result = clazz.result_2_obj( collection.find_one(clazz.map_filter_2_field({'cid': cid}), session=session, **kwargs)) if result and clazz.__cached: CacheEngine.set(cid, result) return result
async def get_by_id(cls, oid, read_preference=None, session=None, **kwargs): """ 依据主键_id获取Document :param oid: 记录主键(_id) :param read_preference :param session: 会话 :return: """ if isinstance(oid, ObjectId): oid = str(oid) result, clazz = None, cls() if clazz.__cached: result = clazz.result_2_obj(CacheEngine.get(oid)) if not result and clazz: result = clazz.result_2_obj( await clazz.get_async_collection(read_preference).find_one( filter=clazz.map_filter_2_field({'_id': ObjectId(oid)}), session=session, **kwargs)) if result and clazz.__cached: CacheEngine.set(oid, result) return result