OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from google.appengine.api import urlfetch |
| 6 import logging |
| 7 |
| 8 import appengine_memcache as memcache |
| 9 |
| 10 class MemcacheUrlFetcher(object): |
| 11 """A wrapper around the App Engine urlfetch module that memcaches results. |
| 12 """ |
| 13 def __init__(self, memcache, base_path): |
| 14 self._memcache = memcache |
| 15 self._base_path = base_path |
| 16 |
| 17 def Fetch(self, url): |
| 18 """Fetches a file synchronously, and memcaches the result for 60 seconds. |
| 19 """ |
| 20 content = self._memcache.Get(url, memcache.MEMCACHE_URL_FETCHER) |
| 21 if content is not None: |
| 22 return content |
| 23 content = urlfetch.fetch(self._base_path + '/' + url).content |
| 24 try: |
| 25 self._memcache.Set(url, content, memcache.MEMCACHE_URL_FETCHER) |
| 26 except Exception as e: |
| 27 logging.info(e) |
| 28 return content |
OLD | NEW |