2017-09-26 82 views
0

我試圖從谷歌文檔Memcache Examples實現僞碼,以便我可以將它傳遞給字典,但我得到的值爲null。我爲解決方案進行了研究,例如,Google App Engine retrieving null values from memcache,但他們沒有幫助。Memcache輸出空值

如何獲得the_id的輸出緩存500秒並返回供update_dict函數使用?我究竟做錯了什麼?

CODE:

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 

OUTPUT:

{ 「ID」:空, 「時間」: 「1506437063」}

回答

0

這個問題已解決。這些問題是:

  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」, 「時間」: 「1506437063」}

{ 「ID」: 「25d853ee-a47d-11e7-8700-69aedf15b2da」, 「time」:「1506437063」}