2014-12-31 4 views
1

Gmail API와 연결되는 Django 앱을 만들고 있는데 google-api-python-client 용 문서 인 httplib2 수준에서 요청을 캐싱하려고합니다. https://developers.google.com/api-client-library/python/guide/performanceMemcached가 Gmail API와 작동하지 않음

문제는 호출이 제대로 이루어지고 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. 

어떤 생각?

+0

이러한 요청을 캐싱하는 요령은 무엇입니까? 매번 최신 결과를 기대해서는 안됩니까? 나는 구글이 캐싱을 막는 헤더를 보내고 있다고 생각한다. –

+0

좋습니다 (GET https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest) 중 하나 이상이 캐시 제어 : 공개, 최대 연령 = 300, 재 검증 필요, 아니요 -transform'을 사용하여 캐시 가능해야합니다. –

+0

@ Alex3917, 코드의 일부분에서 memcache로 캐시하려고하는 것으로 나타 났습니까? Memcache가 캐싱을 전혀하지 않는다고 어떻게 말할 수 있습니까? – Anzel

답변

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 

그래서이 페이지가 즉시 만료되었습니다.

관련 문제