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 17import logging 18 19# This is only an optional dependency because we only import this 20# module when google.appengine.api.memcache is available. 21from google.appengine.api import memcache 22 23from . import base 24from ..discovery_cache import DISCOVERY_DOC_MAX_AGE 25 26 27LOGGER = logging.getLogger(__name__) 28 29NAMESPACE = "google-api-client" 30 31 32class Cache(base.Cache): 33 """A cache with app engine memcache API.""" 34 35 def __init__(self, max_age): 36 """Constructor. 37 38 Args: 39 max_age: Cache expiration in seconds. 40 """ 41 self._max_age = max_age 42 43 def get(self, url): 44 try: 45 return memcache.get(url, namespace=NAMESPACE) 46 except Exception as e: 47 LOGGER.warning(e, exc_info=True) 48 49 def set(self, url, content): 50 try: 51 memcache.set(url, content, time=int(self._max_age), namespace=NAMESPACE) 52 except Exception as e: 53 LOGGER.warning(e, exc_info=True) 54 55 56cache = Cache(max_age=DISCOVERY_DOC_MAX_AGE) 57