【问题标题】:how to re-sort memcache without hitting the database如何在不访问数据库的情况下重新排序 memcache
【发布时间】:2012-07-05 16:53:55
【问题描述】:

我正在使用 jinja2 作为模板系统在 python 中的谷歌应用引擎上编写一个 Web 应用程序,该应用程序基本上允许用户编写帖子/cmets 并对其他用户的帖子进行排名。排名系统基于赞成/反对票的数量和 cmets 的数量。我正在尝试使用 memcache 来存储此计算值并相应地对帖子进行排名。

我只想偶尔将值存储在数据库中,以免写入成本昂贵。我计划每 10 票/cmets 有一个计数器并将其存储在数据库中。我在想这样的事情:

# I update the counter every time that I add a vote or comment
counter = 0
def posts_cache(update = False):
        global counter
        key = 'main'
        posts = memcache.get(key)
        if posts is None or update:
                logging.error("DB QUERY")
                posts = db.GqlQuery("SELECT * "
                                        "FROM Post "
                                        "ORDER BY rank DESC "
                                        "LIMIT 100",
                                         key)
                posts = list(posts)
                memcache.set(key, posts)
        if counter>=10:
                counter = 0
                #put the memcache posts in the database
        return posts

但我不确定如何获取我在 memcache 中的帖子并将它们存储在数据库中。有没有办法在python中做到这一点?我浏览了文档,但没有找到明确的方法。

【问题讨论】:

    标签: python google-app-engine memcached google-cloud-datastore


    【解决方案1】:
    1. 不能这样做,因为 memcache 不可靠,因为它可以驱逐您的实体,而且您会丢失数据。
    2. 批量存储实体不会降低您的成本,您只需按放置的实体付费。
    3. 为了降低您的写作成本,请尝试在不需要索引的属性上设置 indexed=false。

    【讨论】:

    • 我想更多,如果一个帖子获得 3 或 4 票,我只需要放一次。但你说得对,我想这有点太冒险了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多