0

我之前使用过以下代码:

def add_movie(movie_id, title, picture, description):
    movie = Movies(
        id=movie_id,
        title=title,
        picture=picture,
        description=description
        )
    movie.put()

但它不允许我知道电影是否作为新记录添加或现有电影是否已更新。因此,我将代码更改为以下内容:

def add_movie(movie_id, title, picture, description):
    newly_added = True
    movie = Movies.get_by_id(movie_id)
    if movie:
        newly_added = False
    movie.id = movie_id
    movie.title = title
    movie.picture = picture
    movie.description = description
    movie.put()
    return newly_added

但看起来它会先得到电影,然后才会更新它。所以 2 个对数据存储的请求而不是 1 个。

还有其他方法可以做到这一点吗?或者,我错了,从性能的角度来看,这两种方法都是一样的?

4

2 回答 2

3

不,没有其他方法可以解决它。

由于额外的get请求,这会影响性能,但 'get' 非常快速且便宜,所以不用担心。

于 2012-08-11T10:59:43.107 回答
2

As @Peter said: the get operations is very cheap and I wouldn't worry about it, but you can still store the movie id in the memcache and do a primarily check if its there, if its there then the movie is not new, if its not there check the datastore.

于 2012-08-11T12:33:43.563 回答