def _get_client_id(self, client: Client) -> int: if self._client_id_cache is None: with Session() as sess: self._client_id_cache = { Client.FromJSON(row.data): row.id for row in sess.query(DBClient).all() } if client not in self._client_id_cache: with Session() as sess: dbobj = DBClient(data=Client.ToJSON(client)) sess.add(dbobj) sess.flush() self._client_id_cache[client] = dbobj.id return self._client_id_cache[client]