gpt4 book ai didi

google-app-engine - Google App Engine - 本地开发服务器和部署服务器之间的区别

转载 作者:IT王子 更新时间:2023-10-29 02:05:09 25 4
gpt4 key购买 nike

我正在 Google App Engine 上编写应用程序,我已经能够编写在本地运行并将数据放入本地数据存储区的代码。但是,当我将代码部署到谷歌服务器时,没有数据被放入数据存储区。我没有将任何内容放入数据存储区的主要指标是,第一,当我转到开发人员控制台时,我被告知没有条目,第二,当我运行获取数据的页面时,没有返回任何内容。

我是否需要先在 app.yaml 或开发人员控制台中定义新种类,然后它们才能在生产数据存储上工作?

这是我在数据存储中的放置和获取方法。同样,在测试环境中工作,在生产环境中不工作,一切都编译得很好。

package tweetdata

import (
"net/url"
"time"

"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"

"github.com/ChimeraCoder/anaconda"
)

const linkTweetKind string = "LinkTweet"
const tweetKey string = "Tweets"
const tweetKeyID string = "default_tweetstore"

//LinkTweet contains the address extracted from a tweet and the original tweet
type LinkTweet struct {
Address *url.URL
Tweet anaconda.Tweet
}

//StoreTweet is a struct used for storing a tweet in the datastore
type StoreTweet struct {
Address string
Text string
TweetID int64
CreatedTime time.Time
Retweets int
Favorites int
}

//TweetScore is a struct that shows the relative score of an address based on
// it's populatrity
type TweetScore struct {
Address *url.URL
score int
lastActive time.Time
}

//WriteLinkTweet writes a given Tweet to the datastore
func WriteLinkTweet(tweet LinkTweet, c context.Context) error {
log.Infof(c, "Putting Tweet into datastore: %v", tweet.Tweet.Id)
key := datastore.NewIncompleteKey(c, linkTweetKind, getTweetKey(c))
created, _ := tweet.Tweet.CreatedAtTime()
store := &StoreTweet{Address: tweet.Address.String(),
Text: tweet.Tweet.Text,
TweetID: tweet.Tweet.Id,
CreatedTime: created,
Retweets: tweet.Tweet.RetweetCount,
Favorites: tweet.Tweet.FavoriteCount,
}

err := datastore.RunInTransaction(c, func(c context.Context) error {
_, err := datastore.Put(c, key, store)
log.Errorf(c, "Failed to write LinkTweet to datastore. %v", err.Error())
return err
}, nil)
return err
}

//GetAllNewTweets queries the datastore and gets all tweets created since the last
// time given
func GetAllNewTweets(since time.Time, c context.Context) []StoreTweet {
q := datastore.NewQuery(linkTweetKind).Ancestor(getTweetKey(c)).Filter("CreatedTime >=", since)
out := make([]StoreTweet, 0, 15)
q.GetAll(c, &out)
return out
}

// guestbookKey returns the key used for all guestbook entries.
func getTweetKey(c context.Context) *datastore.Key {
// The string "default_guestbook" here could be varied to have multiple guestbooks.
return datastore.NewKey(c, tweetKey, tweetKeyID, 0, nil)
}

最佳答案

您看到了“最终一致性”的(通常最初令人困惑的)效果。在事务之外编写的实体需要一段时间才能对查询可见。

https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/说明问题。 https://cloud.google.com/appengine/docs/go/datastore/transactions是特定去的。

关于google-app-engine - Google App Engine - 本地开发服务器和部署服务器之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31799021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com