| 1 |
# Copyright 2014 Google Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
"""App Engine memcache based cache for the discovery document.""" |
| 16 |
|
| 17 |
import logging |
| 18 |
|
| 19 |
# This is only an optional dependency because we only import this |
| 20 |
# module when google.appengine.api.memcache is available. |
| 21 |
from google.appengine.api import memcache |
| 22 |
|
| 23 |
from . import base |
| 24 |
from ..discovery_cache import DISCOVERY_DOC_MAX_AGE |
| 25 |
|
| 26 |
LOGGER = logging.getLogger(__name__) |
| 27 |
|
| 28 |
NAMESPACE = "google-api-client" |
| 29 |
|
| 30 |
|
| 31 |
class Cache(base.Cache): |
| 32 |
"""A cache with app engine memcache API.""" |
| 33 |
|
| 34 |
def __init__(self, max_age): |
| 35 |
"""Constructor. |
| 36 |
|
| 37 |
Args: |
| 38 |
max_age: Cache expiration in seconds. |
| 39 |
""" |
| 40 |
self._max_age = max_age |
| 41 |
|
| 42 |
def get(self, url): |
| 43 |
try: |
| 44 |
return memcache.get(url, namespace=NAMESPACE) |
| 45 |
except Exception as e: |
| 46 |
LOGGER.warning(e, exc_info=True) |
| 47 |
|
| 48 |
def set(self, url, content): |
| 49 |
try: |
| 50 |
memcache.set(url, content, time=int(self._max_age), namespace=NAMESPACE) |
| 51 |
except Exception as e: |
| 52 |
LOGGER.warning(e, exc_info=True) |
| 53 |
|
| 54 |
|
| 55 |
cache = Cache(max_age=DISCOVERY_DOC_MAX_AGE) |