mutter.monotalk.xyz- Post List バックエンドを Django Content Management System | Wagtail CMS構築しています。
サイト公開後しばらくたちますが、Cache の設定をしていなかったので、Cache の設定をしてみました。
実施したことを記載します。


前提


Wagtail の Cache 設定について

Wagtail Cache検索すると、以下のドキュメントがヒットします。

ドキュメントには、様々な Cache 機能について説明がありますが、今回実施するのは、Cahce ミドルウェアによる API の取得結果の Cache です。
Wagtail の API は Django Rest Framework を使っています。
使用している API の endpoint を拡張して Cache 可能な endpoint を作成します。


Cache の設定手順

以下、Cache 設定のために実施したことを記載します。

1. settings.py に Cache の設定を追加する

Memcached を 他のアプリケーションと共用しているため、KEY_PREFIX付与しました。

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'KEY_PREFIX': 'mutter'
    }
}

2. 拡張エンドポイントの作成

Wagtail の拡張エンドポイントを作成し、Cache デコレータを付与します。
アプリケーションでは、PagesAPIEndpointImagesAPIEndpointDocumentsAPIEndpoint使用しており、それぞれ拡張します。
@method_decorator(cache_page(60*60*4))指定で 4時間 結果をキャッシュします。

  • endpoints.py

    from wagtail.api.v2.endpoints import PagesAPIEndpoint
    from wagtail.wagtailimages.api.v2.endpoints import ImagesAPIEndpoint
    from wagtail.wagtaildocs.api.v2.endpoints import DocumentsAPIEndpoint
    from django.views.decorators.cache import cache_page
    from django.utils.decorators import method_decorator
    
    class CachedPagesAPIEndpoint(PagesAPIEndpoint):
        @method_decorator(cache_page(60*60*4))
        def listing_view(self, request):
            response = super().listing_view(request)
            return response
    
        @method_decorator(cache_page(60*60*4))
        def detail_view(self, request, pk):
            response = super().detail_view(request, pk)
            return response
    
    class CachedImagesAPIEndpoint(ImagesAPIEndpoint):
        @method_decorator(cache_page(60*60*4))
        def listing_view(self, request):
            response = super().listing_view(request)
            return response
    
        @method_decorator(cache_page(60*60*4))
        def detail_view(self, request, pk):
            response = super().detail_view(request, pk)
            return response
    
    class CachedDocumentsAPIEndpoint(DocumentsAPIEndpoint):
        @method_decorator(cache_page(60*60*4))
        def listing_view(self, request):
            response = super().listing_view(request)
            return response
    
        @method_decorator(cache_page(60*60*4))
        def detail_view(self, request, pk):
            response = super().detail_view(request, pk)
            return response
    

  • apis.py
    作成したクラスを、エンドポイントとして登録します。

    from wagtail.api.v2.router import WagtailAPIRouter
    from mutter.endpoints import CachedPagesAPIEndpoint
    from mutter.endpoints import CachedImagesAPIEndpoint
    from mutter.endpoints import CachedDocumentsAPIEndpoint
    
    # Create the router. "wagtailapi" is the URL namespace
    api_router = WagtailAPIRouter('wagtailapi')
    
    # Add the three endpoints using the "register_endpoint" method.
    # The first parameter is the name of the endpoint (eg. pages, images). This
    # is used in the URL of the endpoint
    # The second parameter is the endpoint class that handles the requests
    api_router.register_endpoint('pages', CachedPagesAPIEndpoint)
    api_router.register_endpoint('images', CachedImagesAPIEndpoint)
    api_router.register_endpoint('documents', CachedDocumentsAPIEndpoint)
    

3. Cache の登録確認

過去に作成したスクリプトを使って、Cache の 登録確認を実施しました。
Memcached から echo と nc コマンドを使って、key値/データサイズの有効情報を取得するスクリプトを書いてみた | Monotalk

:1:d0a85dd3c1caaf93d4c941da33589074 142119  2019年  1月 14日 月曜日 23:31:24 JST
:1:be81ad325c6b95f97e5710ff25c7f50b 143967  2019年  1月 14日 月曜日 18:25:59 JST
mutter:1:views.decorators.cache.cache_page..GET.8afb26d942c14fe395e23c16a8b70171.83bf0aec533965b875782854d37402b7.ja.Asia/Tokyo 164851  2019年  1月 14日 月曜日 03:54:57 JST
:1:179100ff48a19e23062a530013919d95 196219  2019年  1月 14日 月曜日 21:54:56 JST
上記の内容で設定されていました。
mutter:1:views.decorators.cache.cache_page..今回登録された Cache です。


参考

以下、参考になりました。

以上です。

コメント