1

我使用 Django 作为我的移动前端的 API。我只是来回发送 JSON。我已经为家庭提要创建了一个端点。每个用户都有一个独特的主页,具体取决于他们关注的人。用户发布一张照片,该照片会被推送到他们所有关注者的主页上。到目前为止非常简单直接。

我的几个同事建议我应该实现某种缓存层,但问题是,这不仅仅是一个静态的常规站点。每个视图都是动态的,基于访问它的用户。

因此,例如,主页提要是按 DESC 时间顺序(从新到旧)发布在平台上的照片列表。

主页提要视图非常基本。每个用户在 Redis 中都有一个 'homefeed:user_id:%s' 列表,其中包含照片对象的主键。我通过 Redis 调用并获取 request.user 的 homefeed 列表,然后使用该列表在数据库中查询这些对象,如下所示:

homefeed_pk_list = redis_server.lrange('homefeed:user_id:%s' % request.user.pk, 0, 100)

# Home feed queryset
queryset = Photo.objects.filter(pk__in = homefeed_pk_list)
response_data= []
for photo in queryset:
       # Code to return back JSON data
return HttpResponse(json.dumps(response_data), content_type="application/json")    

很简单。现在我的问题是,在这种情况下,缓存的最佳做法应该是什么?我可以单独缓存每个序列化的照片对象并将到期时间设置为 24 小时,因为某些照片对象位于多个提要中(用户。如果缓存中不存在该对象,我将访问数据库。您如何看待这种方法?

4

1 回答 1

1

为了获得最佳性能,您可以实现类似于Russian Doll Caching的东西,其摘要类似于:缓存对象、缓存这些对象的列表、缓存包含该列表的生成页面(即,不要只缓存完成的结果,一直缓存下来)。

但是,鉴于您的示例,我可能会从以下内容开始:

import hashlib

from django.core.cache import cache
from django.http import HttpResponse

from whereever import redis_server


def feed(request):
    """
    Returns a JSON response containing Photo data.
    """
    # Get the list of PKs from Redis
    photo_pks = redis_server.lrange(
        'homefeed:user_id:%d' % request.user.pk,
        0,
        100
    )

    # Make a SHA1 hash of the PKs (cache key)
    cach_key = hashlib.sha1(unicode(photo_pks)).hexdigest()

    # Get the existing cache
    content = cache.get(cach_key)

    if content is None:
        # Make a queryset of Photos using the PK list
        queryset = Photo.objects.filter(pk__in=photo_pks)

        # Use .values() to get a list of dicts (the response data)
        content = json.dumps(
            queryset.values('pk', 'url', 'spam', 'eggs')
        )

        # Cache the response string for 24 hours
        cache.set(cach_key, content, 60 * 60 * 24)

    return HttpResponse(content, content_type='application/json')

结果将是响应内容将被缓存 24 小时,或者直到 Redis 中的 PK 列表(可能是在其他地方设置并在添加新照片时更新等)发生变化,因为缓存键是使用PK名单。

于 2014-10-07T01:15:49.880 回答