We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Add a new mechanism to avoid calling the legacy memcache API
This API is only supported on the python27 runtime.  Lets only try to import it in environments that actually support it.

The problem I'm running into is a bit esoteric, but here goes.  We've built some GAE API shims to help move our apps to newer App Engine runtimes (like python3) that don't include these legacy APIs.  Because of that, when this library tries to import google.appengine.api.memcache, it imports and uses our shim, and that isn't always desirable.  Having some way to configure googleapiclient to not use this legacy API even if it is importable would be useful.

Despite this sorta niche use case, I figured I'd propose this change upstream since reducing reliance on a py2-only API shouldn't be too controversial these days.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `googleapiclient/discovery_cache/__init__.py`
Content:
```
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 """Caching utility for the discovery document."""
16 
17 from __future__ import absolute_import
18 
19 import logging
20 import datetime
21 
22 
23 LOGGER = logging.getLogger(__name__)
24 
25 DISCOVERY_DOC_MAX_AGE = 60 * 60 * 24  # 1 day
26 
27 
28 def autodetect():
29     """Detects an appropriate cache module and returns it.
30 
31   Returns:
32     googleapiclient.discovery_cache.base.Cache, a cache object which
33     is auto detected, or None if no cache object is available.
34   """
35     try:
36         from google.appengine.api import memcache
37         from . import appengine_memcache
38 
39         return appengine_memcache.cache
40     except Exception:
41         try:
42             from . import file_cache
43 
44             return file_cache.cache
45         except Exception as e:
46             LOGGER.warning(e, exc_info=True)
47             return None
48
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/googleapiclient/discovery_cache/__init__.py b/googleapiclient/discovery_cache/__init__.py
--- a/googleapiclient/discovery_cache/__init__.py
+++ b/googleapiclient/discovery_cache/__init__.py
@@ -18,7 +18,7 @@
 
 import logging
 import datetime
-
+import os
 
 LOGGER = logging.getLogger(__name__)
 
@@ -32,16 +32,18 @@
     googleapiclient.discovery_cache.base.Cache, a cache object which
     is auto detected, or None if no cache object is available.
   """
-    try:
-        from google.appengine.api import memcache
-        from . import appengine_memcache
-
-        return appengine_memcache.cache
-    except Exception:
+    if 'APPENGINE_RUNTIME' in os.environ:
         try:
-            from . import file_cache
+            from google.appengine.api import memcache
+            from . import appengine_memcache
+
+            return appengine_memcache.cache
+        except Exception:
+            pass
+    try:
+        from . import file_cache
 
-            return file_cache.cache
-        except Exception as e:
-            LOGGER.warning(e, exc_info=True)
-            return None
+        return file_cache.cache
+    except Exception as e:
+        LOGGER.warning(e, exc_info=True)
+        return None

{"golden_diff": "diff --git a/googleapiclient/discovery_cache/__init__.py b/googleapiclient/discovery_cache/__init__.py\n--- a/googleapiclient/discovery_cache/__init__.py\n+++ b/googleapiclient/discovery_cache/__init__.py\n@@ -18,7 +18,7 @@\n \n import logging\n import datetime\n-\n+import os\n \n LOGGER = logging.getLogger(__name__)\n \n@@ -32,16 +32,18 @@\n     googleapiclient.discovery_cache.base.Cache, a cache object which\n     is auto detected, or None if no cache object is available.\n   \"\"\"\n-    try:\n-        from google.appengine.api import memcache\n-        from . import appengine_memcache\n-\n-        return appengine_memcache.cache\n-    except Exception:\n+    if 'APPENGINE_RUNTIME' in os.environ:\n         try:\n-            from . import file_cache\n+            from google.appengine.api import memcache\n+            from . import appengine_memcache\n+\n+            return appengine_memcache.cache\n+        except Exception:\n+            pass\n+    try:\n+        from . import file_cache\n \n-            return file_cache.cache\n-        except Exception as e:\n-            LOGGER.warning(e, exc_info=True)\n-            return None\n+        return file_cache.cache\n+    except Exception as e:\n+        LOGGER.warning(e, exc_info=True)\n+        return None\n", "issue": "Add a new mechanism to avoid calling the legacy memcache API\nThis API is only supported on the python27 runtime.  Lets only try to import it in environments that actually support it.\r\n\r\nThe problem I'm running into is a bit esoteric, but here goes.  We've built some GAE API shims to help move our apps to newer App Engine runtimes (like python3) that don't include these legacy APIs.  Because of that, when this library tries to import google.appengine.api.memcache, it imports and uses our shim, and that isn't always desirable.  Having some way to configure googleapiclient to not use this legacy API even if it is importable would be useful.\r\n\r\nDespite this sorta niche use case, I figured I'd propose this change upstream since reducing reliance on a py2-only API shouldn't be too controversial these days.\n", "before_files": [{"content": "# Copyright 2014 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Caching utility for the discovery document.\"\"\"\n\nfrom __future__ import absolute_import\n\nimport logging\nimport datetime\n\n\nLOGGER = logging.getLogger(__name__)\n\nDISCOVERY_DOC_MAX_AGE = 60 * 60 * 24  # 1 day\n\n\ndef autodetect():\n    \"\"\"Detects an appropriate cache module and returns it.\n\n  Returns:\n    googleapiclient.discovery_cache.base.Cache, a cache object which\n    is auto detected, or None if no cache object is available.\n  \"\"\"\n    try:\n        from google.appengine.api import memcache\n        from . import appengine_memcache\n\n        return appengine_memcache.cache\n    except Exception:\n        try:\n            from . import file_cache\n\n            return file_cache.cache\n        except Exception as e:\n            LOGGER.warning(e, exc_info=True)\n            return None\n", "path": "googleapiclient/discovery_cache/__init__.py"}], "after_files": [{"content": "# Copyright 2014 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Caching utility for the discovery document.\"\"\"\n\nfrom __future__ import absolute_import\n\nimport logging\nimport datetime\nimport os\n\nLOGGER = logging.getLogger(__name__)\n\nDISCOVERY_DOC_MAX_AGE = 60 * 60 * 24  # 1 day\n\n\ndef autodetect():\n    \"\"\"Detects an appropriate cache module and returns it.\n\n  Returns:\n    googleapiclient.discovery_cache.base.Cache, a cache object which\n    is auto detected, or None if no cache object is available.\n  \"\"\"\n    if 'APPENGINE_RUNTIME' in os.environ:\n        try:\n            from google.appengine.api import memcache\n            from . import appengine_memcache\n\n            return appengine_memcache.cache\n        except Exception:\n            pass\n    try:\n        from . import file_cache\n\n        return file_cache.cache\n    except Exception as e:\n        LOGGER.warning(e, exc_info=True)\n        return None\n", "path": "googleapiclient/discovery_cache/__init__.py"}]}