在Dynamodb中缓存有什么问题?

时间:2012-09-12 22:59:20

标签: python caching network-programming

我有一个访问Web服务(DynamoDB)的Python应用程序。它执行15,000次POST操作,但每秒限制为1,000次。

添加日志记录后,我注意到每个POST都在资源刷新之前返回,始终返回相同的信息。

以下是日志的内容:

DynamoDB_20111205.DescribeTable: id=... time=27ms
DynamoDB_20111205.PutItem: id=... time=22ms
DynamoDB_20111205.DescribeTable: id=... time=27ms
DynamoDB_20111205.PutItem: id=... time=22ms
DynamoDB_20111205.DescribeTable: id=... time=27ms
DynamoDB_20111205.PutItem: id=... time=22ms
DynamoDB_20111205.DescribeTable: id=... time=27ms
DynamoDB_20111205.PutItem: id=... time=22ms

与预期的对比:

DynamoDB_20111205.DescribeTable: id=... time=27ms
DynamoDB_20111205.PutItem: id=... time=22ms
DynamoDB_20111205.PutItem: id=... time=22ms
DynamoDB_20111205.PutItem: id=... time=22ms
DynamoDB_20111205.PutItem: id=... time=22ms

当然,我的反应是缓存“DescribeTable”操作的结果,因为它始终是相同的(除了我不关心的一些位)并且对性能“增益”感到好奇。以下是我测试的大小顺序:

  • 没有缓存:37.5秒
  • 班级缓存:41.8秒(由所有实例共享)
  • 实例级缓存:41.7秒

我在相似条件下重复了6次,但没有尝试任何“科学质量”测量。

编辑:这是我的代码的精简版本。这是高度简化的。在真实版本中,我使用未发布的Boto版本,DynamoDB-mapper和gevent的greenlets

#!/usr/bin/env python

class RemoteTableInfo(objects):
    def __init__(self):
        load_remote_table_info() # Triggers the "DescribeTable" in logs

class Item(objects):
    def save(self, table, ...):
        do_some_IO() # Triggers the "PutItem" in logs

class ObjectMapper_with_cache(object):
    table = None

    def __init__(self):
        cls = type(self)
        if cls.table is None:
            self.table = RemoteTableInfo()

    def do_work(self):
        cls = type(self)
        Item(cls.table, ...).save()

class ObjectMapper_no_cache(object):
    def do_work(self):
        table = RemoteTableInfo()
        Item(table, ...).save()

# Slow version
for i in range(15000):
    ObjectMapper_with_cache().do_work()

# Fast version
for i in range(15000):
    ObjectMapper_no_cache().do_work()

为什么版本没有缓存更快 4秒 我的“最聪明”尝试(班级)?

注意:我真的相信这与Python的内部结构有关,而不是与底层框架有关

0 个答案:

没有答案