Memcached没有使用gmail API

时间:2014-12-31 21:48:34

标签: python memcached gmail-api httplib2

我正在创建一个与gmail API接口的Django应用程序,我正在尝试在httplib2级别缓存请求,google-api-python-client的文档:https://developers.google.com/api-client-library/python/guide/performance

问题在于,当调用正常,并且python能够连接到memcached时,看起来在memcached中找不到键(如果我正确读取memcached输出)。代码如下所示:

from django.core.cache import cache

flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http(cache=cache)

credentials = STORAGE.get()
if credentials is None or credentials.invalid:
    credentials = run(flow, STORAGE, http=http)

http = credentials.authorize(http)
gmail_client = build('gmail', 'v1', http=http)
batch = BatchHttpRequest()

messages = gmail_client.users().messages().list(userId='me', maxResults=1).execute()

if messages['messages']:
    for message in messages['messages']:
        batch.add(gmail_client.users().messages().get(userId='me', id=message['id'], format='metadata', fields="payload,threadId,id", metadataHeaders=['subject','date','to','from']), callback=messageCallback)

batch.execute()

以下是memcached日志的内容:

## First time running the http request

<27 new auto-negotiating client connection
27: Client using the ascii protocol
<27 get :1:https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest
>27 END
<27 set :1:https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest 0 300 53713
>27 STORED
<27 get :1:https://www.googleapis.com/gmail/v1/users/me/messages?alt=json&maxResults=1
>27 END
<27 get :1:https://accounts.google.com/o/oauth2/token
>27 END
<27 delete :1:https://accounts.google.com/o/oauth2/token
>27 NOT_FOUND
<27 get :1:https://www.googleapis.com/gmail/v1/users/me/messages?alt=json&maxResults=1
>27 END
<27 delete :1:https://www.googleapis.com/gmail/v1/users/me/messages?alt=json&maxResults=1
>27 NOT_FOUND
<27 get :1:https://www.googleapis.com/batch
>27 END
<27 delete :1:https://www.googleapis.com/batch
>27 NOT_FOUND
<27 quit
<27 connection closed.


## Second time running the http request

<27 new auto-negotiating client connection
27: Client using the ascii protocol
<27 get :1:https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest
>27 sending key :1:https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest
>27 END
<27 get :1:https://www.googleapis.com/gmail/v1/users/me/messages?alt=json&maxResults=1
>27 END
<27 delete :1:https://www.googleapis.com/gmail/v1/users/me/messages?alt=json&maxResults=1
>27 NOT_FOUND
<27 get :1:https://www.googleapis.com/batch
>27 END
<27 delete :1:https://www.googleapis.com/batch
>27 NOT_FOUND
<27 quit
<27 connection closed.

知道发生了什么事吗?

1 个答案:

答案 0 :(得分:1)

缓存工作正常。但是,第一个请求可能是发送允许缓存的缓存头的唯一请求。

这些日志行:

<27 get :1:https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest
>27 sending key :1:https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest

表示缓存工作正常。此请求支持缓存,因为此页面会发送标头Cache-Control: public, max-age=300, must-revalidate, no-transform

https://www.googleapis.com/batch之类的其他请求会发送Cache-Control: no-cache, no-store, max-age=0, must-revalidate之类的标题。

https://www.googleapis.com/gmail/v1/users/me/messages?alt=json&maxResults=1发送:

Date: Wed, 31 Dec 2014 22:05:35 GMT
Expires: Wed, 31 Dec 2014 22:05:35 GMT
Cache-Control: private, max-age=0

所以除非您在同一秒内发送另一个请求,否则此页面会立即过期。