googleapiclient.discovery_cache.appengine_memcache

Package googleapiclient :: Package discovery_cache :: Module appengine_memcache |
[hide private]
[frames] | no frames]

# Source Code for Module googleapiclient.discovery_cache.appengine_memcache

     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  NAMESPACE = 'google-api-client'
    27
    28
    29 -class Cache(base.Cache):
    30    """A cache with app engine memcache API."""
    31
    32 -  def __init__(self, max_age):
    33        """Constructor.
    34
    35        Args:
    36          max_age: Cache expiration in seconds.
    37        """
    38        self._max_age = max_age
    39
    40 -  def get(self, url):
    41      try:
    42        return memcache.get(url, namespace=NAMESPACE)
    43      except Exception as e:
    44        logging.warning(e, exc_info=True)
    45
    46 -  def set(self, url, content):
    47      try:
    48        memcache.set(url, content, time=int(self._max_age), namespace=NAMESPACE)
    49      except Exception as e:
    50        logging.warning(e, exc_info=True)
    51
    52  cache = Cache(max_age=DISCOVERY_DOC_MAX_AGE)
    53