Memcache 输出空值

Memcache Outputting Null Value

我正在尝试实现 Google 文档 Memcache Examples, so that I can pass it to a dictionary but I am getting a null value. I've researched for solutions, for example, Google App Engine retrieving null values from memcache 中的伪代码,但它们没有帮助。

如何让 the_id 的输出缓存 500 秒并返回供 update_dict 函数使用?我做错了什么?

代码:

def return_id(self):
    the_id = str(uuid.uuid1())

    data = memcache.get(the_id)
    print data

    if data is not None:
        return data
    else:
        memcache.add(the_id, the_id, 500)

    return data

def update_dict(self):
    ....
    id = self.return_id()
    info = {
        'id': id,
        'time': time
    }

    info_dump = json.dumps(info)
    return info_dump

输出:

{"id": null, "time": "1506437063"}

此问题已解决。问题是:

  1. 我的密钥没有正确的字符串名称'the_id'
  2. 我没有在 else 语句中传递数据

解法:

....
the_id = str(uuid.uuid1())

data = memcache.get('the_id') #fix: pass a string for the key name
print data

if data is not None:
    return data
else:
    data = the_id #fix: added the object that needed to be passed to data
    memcache.add('the_id', the_id, 500)
return data
....

输出:

{"id": "25d853ee-a47d-11e7-8700-69aedf15b2da", "time": "1506437063"}

{"id": "25d853ee-a47d-11e7-8700-69aedf15b2da", "time": "1506437063"}