django - 如何在 Django 中使用缓存? (最好在GAE中)

标签 django google-app-engine memcached

我无法让 memcached 与 GAE 一起工作。 当我使用 Google 缓存后端时,请按照 GAE website 上的教程进行操作, View 不会被缓存。所以我按照 Django 教程中的建议使用了缓存 url(例如:

`(r'^example$', cache_page(60*15)(views.example)),

然后我明白了:

File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/middleware/cache.py", line 205, in __init__
self.cache_timeout = self.cache.default_timeout

 AttributeError: 'Client' object has no attribute 'default_timeout'

AttributeError: 'Client' 对象没有属性 'default_timeout',如果我使用谷歌后端 ( django.core.cache.backends.memcached.MemcachedCache ) 我得到

Error: ImproperlyConfigured: Error importing middleware django.contrib.sessions.middleware: "No module named memcache".

这里有人之前问过关于使用 Django 缓存后端的问题,并建议安装 python-memcached,我这样做了,但它仍然不起作用。

有人建议为 GAE 编写后端。我不太明白。如果对这个问题的最佳回答是逐步解释如何非常粗略地编写后端,那么我会接受这个答案。

最佳答案

并非 Django 的所有功能都适用于 App Engine。因此,由于 App Engine 基础架构限制,您尝试使用的功能对于 App Engine Django 库是不允许的。

如果我没理解错的话,你想缓存整个页面,或者换句话说,整个 View 的响应?您可以通过这种方式执行此操作(只是示例):

# Django on App Engine view example
from google.appengine.api import memcache
from django.http import HttpResponse

def cached_index_page(request):
  output_html = memcache.get('index_page')  # here we "take" from cache
  if output is not None:
    pass
  else:
    output_html = get_page_content()
    memcache.add('index_page', output_html, 60)  # here we "put" to cache" (timeout also here)
  HttpResponse(output_html)

为了您的目的,您可以创建 Django 的中间件,自动缓存您需要的任何页面。

同时确保您从配置文件中删除了 App Engine 上所有不相关/ Not Acceptable 内容。 考虑帮助页面 ( https://developers.google.com/appengine/articles/django ),最小配置如下所示:

import os

# 'project' refers to the name of the module created with django-admin.py
ROOT_URLCONF = 'project.urls'

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
#    'django.contrib.sessions.middleware.SessionMiddleware',
#    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'google.appengine.ext.ndb.django_middleware.NdbDjangoMiddleware', # NoSQL db middleware
)

INSTALLED_APPS = (
#    'django.contrib.auth',
    'django.contrib.contenttypes',
#    'django.contrib.sessions',
    'django.contrib.sites',
)

ROOT_PATH = os.path.dirname(__file__)
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or
    # "C:/www/django/templates".  Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    ROOT_PATH + '/templates',
)

请记住,App Engine 有自己的本地缓存,例如Python 运行时环境在单个 Web 服务器上的请求之间缓存导入的模块,除了导入的模块之外,您可以告诉 App Engine 缓存 CGI 处理程序脚本本身。

有用的链接: https://developers.google.com/appengine/articles/django-nonrelhttps://developers.google.com/appengine/docs/python/tools/libraries27

关于django - 如何在 Django 中使用缓存? (最好在GAE中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14704427/

相关文章:

python - Django + uWSGI + Websocket 客户端连接

Django 测试用例 : SimpleUploadedFile shows wrong filetype

python - 南值错误: Cannot successfully create field for model: 'module' object has no attribute <CustomClassAttributeHere>

google-app-engine - 为什么 GCP Cloud Build 会出现系统性失败以及如何修复?

python - GAE——祖先问题

数据库触发器/参照完整性和内存缓存

python : email sending failing on SSL read

python - 如何更改 python 应用程序引擎中日志消息的默认格式?

java - 如何使用redis或memcached配置tomcat6或7共享 session ?

google-app-engine - 在 App Engine Memcache 上实现 get_multi