我正在使用 python 和 jinja2 在谷歌应用引擎上构建一个 Web 应用程序。我有一个网站,用户可以在其中写帖子,我有 15 个主要类别,每个类别都有 4 个部门。现在我想实现 memcache,因为我有 20:1 的读者与海报的比例,但是如果不制作 60 个不同的键,我怎么能做到呢?我应该那样做吗?或者我应该点击数据库并对结果进行排序并在函数上有一些参数来获得这些结果,如下所示:
def posts_cache(update = False, category = None, sport = None):
key = 'main'
posts = memcache.get(key)
if posts is None or update:
logging.error("DB QUERY")
posts = db.GqlQuery("SELECT * "
"FROM Post "
"ORDER BY created DESC "
"LIMIT 100",
key)
posts = list(posts)
memcache.set(key, posts)
if category and sport:
sportcatlist = []
for post in posts:
if post.category == category:
if post.sport == sport:
sportcatlist.append(post)
return sportcatlist
elif category:
categorylist = []
for post in posts:
if post.category == category:
categorylist.append(post)
return categorylist
elif sport:
sportlist = []
for post in posts:
if post.sport == sport:
sportlist.append(post)
return sportlist
return posts
还是有更有效的方法来做到这一点?