python - 如何在不影响数据库的情况下重新排序内存缓存

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

我正在使用 jinja2 作为模板系统使用 jinja2 作为模板系统在 Google 应用程序引擎上编写一个 Web 应用程序,该应用程序基本上允许用户编写帖子/评论并对其他用户的帖子进行排名。排名系统基于赞成/反对票数和评论数。我正在尝试使用内存缓存来存储这个计算值并对帖子进行相应的排名。

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

# 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

但我不知道如何获取内存缓存中的帖子并将其存储在数据库中。有什么方法可以在 python 中做到这一点吗?我已经浏览了文档,但没有找到明确的方法。

最佳答案

  1. 不能这样做,因为内存缓存并不可靠,因为它可以驱逐您的实体并且您会丢失数据。
  2. 批量存储实体并不会降低您的成本,您只需为每个实体存储付费。
  3. 为了降低编写成本,请尝试对不需要索引的属性设置 indexed=false。

关于python - 如何在不影响数据库的情况下重新排序内存缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11348989/

相关文章:

php - 帮助 PHP call_user_func 并将函数集成到类中?

java - AppEngine 响应时间差异

java - Google AppEngine + 本地 JUnit 测试 + Jersey 框架 + 嵌入式 Jetty

python - 在 Django 应用程序中实现 memcached 需要哪些步骤?

python - 使用 Memcache 缓存 Matplotlib(Wont Pickle)

python - WSGI - 将内容类型设置为 JSON

python - 为什么我的功能会自动执行?

python - 如何阅读周三 Oct 02 09 :37:43 BST 2019 to datetime format using pandas?

python - Django 1.7 makemigrations 不工作 - 没有名为的列

python urllib2 自动填表及检索结果