My favorites | 中文(简体) | Sign in

概述

Memcache 服务为您的应用程序提供了高性能的内存键值缓存,您可通过应用程序的多个实例访问该缓存。Memcache 对于那些不需要数据库的永久性功能和事务功能的数据很有用,例如临时数据或从数据库复制到缓存以进行高速访问的数据。Memcache API 与 Danga Interactive 开发的 Memcached 有类似的功能并兼容。

Memcache API 可通过以下方式让您提高应用程序的性能并减少数据库的负载:

  • 显著地减少数据库查询的次数。
  • 减少使用率非常高的页面的数据库配额的使用。
  • 缓存操作量巨大的查询和操作的结果。
  • 让使用临时计数器成为可能。

通过使用 Memcache API,您可以为应用程序中的数据创建一致的缓存。缓存可用于应用程序中的所有实例,而且数据只有通过内存压力(例如缓存中的数据过多)或开发人员设置的缓存政策才能清除。可以在存储在缓存中的每个键-值对上设置缓存政策。您可以清除所有缓存或针对每份数据设置缓存过期时间。

from google.appengine.api import memcache

# Add a value if it doesn't exist in the cache, with a cache expiration of 1 hour.
memcache.add(key="weather_USA_98105", value="raining", time=3600)

# Set several values, overwriting any existing values for these keys.
memcache.set_multi({ "USA_98105": "raining",
                     "USA_94105": "foggy",
                     "USA_94043": "sunny" },
                     key_prefix="weather_", time=3600)

# Atomically increment an integer value.
memcache.set(key="counter", 0)
memcache.incr("counter")
memcache.incr("counter")
memcache.incr("counter")