我有以下非常简单的代码来将字符串保留在datastore
. 我从各种datastore
例子中总结出来,但我仍然对它不满意。
它的目的只是将一个字符串存储在一个键下persist
并在 下检索它fromPresistence
。
// Used to store the string value.
type Entity struct {
Value string
}
// Grow my key.
func key(x string) *datastore.Key {
return datastore.NewKey(context, "Persist", x, 0, nil)
}
// Get it from persistence storage.
func fromPersistence(x string) string {
var persisted string = x
// Make my key.
k := key(x)
// New entity for filling in.
e := new(Entity)
// Look it up!
if err := datastore.Get(context, k, e); err == nil {
// It was there!
persisted = e.Value
context.Debugf("Persisted %s=%s", x, persisted)
}
return persisted
}
// Persist the latest number.
func persist(x string) func(*big.Int) {
return func(n *big.Int) {
// Make my key.
k := key(x)
// New entity for filling in.
e := new(Entity)
// Value is the decimal form of the number.
e.Value = n.String()
context.Debugf("Persist %s=%s", start, e.Value)
if _, err := datastore.Put(context, k, e); err != nil {
context.Debugf("Persist failed! %s", err)
}
}
}
但似乎每次都落后一个。以下是一些日志结果:
C:\Go\GAE\go_appengine\google\src>\go\gae\go_appengine\dev_appserver.py unique/
INFO 2013-10-15 20:55:08,296 sdk_update_checker.py:245] Checking for updates to the SDK.
INFO 2013-10-15 20:55:09,726 api_server.py:138] Starting API server at: http://localhost:50218
INFO 2013-10-15 20:55:09,746 dispatcher.py:168] Starting module "default" running at: http://localhost:8080
INFO 2013-10-15 20:55:09,763 admin_server.py:117] Starting admin server at: http://localhost:8000
INFO 2013-10-15 20:56:22,131 module.py:599] default: "GET / HTTP/1.1" 304 -
INFO 2013-10-15 20:56:22,344 module.py:599] default: "GET /favicon.ico HTTP/1.1" 304 -
INFO 2013-10-15 20:56:22,448 module.py:599] default: "GET /favicon.ico HTTP/1.1" 304 -
2013/10/15 20:56:26 DEBUG: Persisted 38913371956013078496870267859=3378577588146889866220112993
2013/10/15 20:56:26 DEBUG: Persist 38913371956013078496870267859=21186844412818184262771263024
...
2013/10/15 20:56:30 DEBUG: Persist 38913371956013078496870267859=1324177775801136516423203939
INFO 2013-10-15 20:56:30,756 module.py:599] default: "GET /n HTTP/1.1" 200 19
INFO 2013-10-15 20:56:30,927 module.py:599] default: "GET /favicon.ico HTTP/1.1" 304 -
2013/10/15 20:56:32 DEBUG: Persist 38913371956013078496870267859=20778614526287997725322370609
C:\Go\GAE\go_appengine\google\src>\go\gae\go_appengine\dev_appserver.py unique/
INFO 2013-10-15 20:57:15,657 sdk_update_checker.py:245] Checking for updates to the SDK.
INFO 2013-10-15 20:57:17,033 api_server.py:138] Starting API server at: http://localhost:50241
INFO 2013-10-15 20:57:17,085 dispatcher.py:168] Starting module "default" running at: http://localhost:8080
INFO 2013-10-15 20:57:17,098 admin_server.py:117] Starting admin server at: http://localhost:8000
2013/10/15 20:57:24 DEBUG: Persisted 38913371956013078496870267859=1324177775801136516423203939
2013/10/15 20:57:24 DEBUG: Persist 38913371956013078496870267859=20778614526287997725322370609
...
查看最后一次持久性尝试如何存储20778614526287997725322370609但检索尝试返回之前的持久值1324177775801136516423203939。
我究竟做错了什么?
注意:我已更改代码以使用context.Debugf
打印调试字符串的机制来排除奇怪的日志记录。
旧Logf
代码如下。我确信这是奇怪日志条目的原因。这不是我的问题的目标。我会自己解决这个问题。
func Logf(format string, a ...interface{}) {
if context != nil {
// Context is valid.
if len(logQueue) > 0 {
// Roll out the stored entries.
for i := 0; i < len(logQueue); i++ {
context.Debugf("%s", logQueue[i])
}
// Empty the queue.
logQueue = make([]string, 0)
}
// Pass a "" to just flush the queue
if format != "" {
// Log it through the context.
context.Debugf(format, a)
}
} else {
// No context! Queue it up.
logQueue = append(logQueue, fmt.Sprintf(format, a...))
}
}