-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented automatic caching for the discovery documents.
- Loading branch information
Takashi Matsuo
committed
Aug 20, 2015
1 parent
a98add2
commit 40a4bfa
Showing
6 changed files
with
292 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2014 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Caching utility for the discovery document.""" | ||
|
||
|
||
from __future__ import absolute_import | ||
|
||
import logging | ||
import datetime | ||
|
||
|
||
DISCOVERY_DOC_MAX_AGE = 60 * 60 * 24 # 1 day | ||
|
||
|
||
def autodetect(): | ||
"""Detects an appropriate cache module and returns it. | ||
Returns: | ||
An object with the following methods; get(url), set(url, content). | ||
""" | ||
try: | ||
from google.appengine.api import memcache | ||
from . import appengine_memcache | ||
return appengine_memcache.Cache(max_age = DISCOVERY_DOC_MAX_AGE) | ||
except: | ||
try: | ||
from . import file_cache | ||
return file_cache.Cache(max_age = DISCOVERY_DOC_MAX_AGE) | ||
except Exception as e: | ||
logging.warning(e, exc_info=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright 2014 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""App Engine memcache based cache for the discovery document.""" | ||
|
||
|
||
import logging | ||
|
||
# This is only an optional dependency because we only import this | ||
# module when google.appengine.api.memcache is available. | ||
from google.appengine.api import memcache | ||
|
||
from . import base | ||
|
||
NAMESPACE = 'google-api-client' | ||
|
||
|
||
class Cache(base.CacheBase): | ||
"""A cache with app engine memcache API. | ||
Attributes: | ||
max_age: Cache expiration in seconds. | ||
""" | ||
def __init__(self, max_age): | ||
"""Constructor for appengine_memcache.Cache. | ||
Args: | ||
max_age: Cache expiration in seconds. | ||
""" | ||
self.max_age = max_age | ||
|
||
def get(self, url): | ||
"""Gets the content from the memcache with a given key. | ||
Args: | ||
url: string, the key for the cache. | ||
""" | ||
try: | ||
return memcache.get(url, namespace=NAMESPACE) | ||
except Exception as e: | ||
logging.warning(e, exc_info=True) | ||
|
||
def set(self, url, content): | ||
"""Sets the given key and content in the cache. | ||
Args: | ||
url: string, the key for the cache. | ||
content: string, the discovery document. | ||
""" | ||
try: | ||
memcache.set(url, content, time=int(self.max_age), namespace=NAMESPACE) | ||
except Exception as e: | ||
logging.warning(e, exc_info=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2014 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""An abstract class for caching the discovery document.""" | ||
|
||
|
||
import abc | ||
|
||
|
||
class CacheBase(object): | ||
"""A base abstract cache class.""" | ||
__metaclass__ = abc.ABCMeta | ||
|
||
@abc.abstractmethod | ||
def get(self, url): | ||
"""Gets the content from the memcache with a given key. | ||
Args: | ||
url: string, the key for the cache. | ||
""" | ||
return | ||
|
||
@abc.abstractmethod | ||
def set(self, url, content): | ||
"""Sets the given key and content in the cache. | ||
Args: | ||
url: string, the key for the cache. | ||
content: string, the discovery document. | ||
""" | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright 2014 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""File based cache for the discovery document.""" | ||
|
||
|
||
import datetime | ||
import json | ||
import logging | ||
import os | ||
import tempfile | ||
import threading | ||
|
||
from . import base | ||
|
||
|
||
FILENAME = 'discovery-doc.cache' | ||
EPOCH = datetime.datetime.utcfromtimestamp(0) | ||
|
||
|
||
def to_timestamp(d): | ||
return (d - EPOCH).total_seconds() | ||
|
||
|
||
class Cache(base.CacheBase): | ||
"""A file based cache for the discovery documents. | ||
Attributes: | ||
max_age: Cache expiration in seconds. | ||
lock: threading.Lock, to make the operations thread safe. | ||
cache_file: string, full path to the cache file. | ||
""" | ||
def __init__(self, max_age): | ||
"""Constructor for appengine_memcache.Cache. | ||
Args: | ||
max_age: Cache expiration in seconds. | ||
""" | ||
self.max_age = max_age | ||
self.lock = threading.Lock() | ||
self.cache_file = os.path.join(tempfile.gettempdir(), FILENAME) | ||
if not os.path.isfile(self.cache_file): | ||
with open(self.cache_file, 'w+') as f: | ||
json.dump({}, f) | ||
|
||
def get(self, url): | ||
"""Gets the content from the memcache with a given key. | ||
Args: | ||
url: string, the key for the cache. | ||
""" | ||
try: | ||
with self.lock: | ||
with open(self.cache_file, 'r') as f: | ||
cache = json.load(f) | ||
if url in cache: | ||
content, t = cache.get(url, (None, 0)) | ||
if to_timestamp(datetime.datetime.now()) < t + self.max_age): | ||
return content | ||
except Exception as e: | ||
logging.warning(e, exc_info=True) | ||
|
||
def set(self, url, content): | ||
"""Sets the given key and content in the cache. | ||
Args: | ||
url: string, the key for the cache. | ||
content: string, the discovery document. | ||
""" | ||
try: | ||
with self.lock: | ||
with open(self.cache_file, 'r+') as f: | ||
cache = json.load(f) | ||
cache[url] = (content, to_timestamp(datetime.datetime.now())) | ||
f.seek(0) | ||
json.dump(cache, f) | ||
except Exception as e: | ||
logging.warning(e, exc_info=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters