应用程序负责从存储中读写。缓存不直接与存储交互。应用程序执行以下操作:
在缓存中查找条目,导致缓存未命中
从数据库加载条目
将条目添加到缓存
返回条目
def get_user(self, user_id): user = cache.get("user.{0}", user_id) if user is None: user = db.query("SELECT * FROM users WHERE user_id = {0}", user_id) if user is not None: key = "user.{0}".format(user_id) cache.set(key, json.dumps(user)) return user
通常以这种方式使用 Memcached。添加到缓存中的数据的后续读取很快。缓存旁路也称为延迟加载。只缓存请求的数据,这避免了用未请求的数据填满缓存。

用自己的话写下这一站的结论。