Refactor cache control

This commit is contained in:
Akira Tempaku 2025-05-25 19:41:59 +09:00
commit f6929c47cd
Signed by: paku
GPG key ID: 5B4E8402BCC50607
7 changed files with 64 additions and 67 deletions
src/lib

View file

@ -3,30 +3,62 @@
(:import-from #:microcms
#:define-list-client
#:define-object-client)
(:import-from #:function-cache
#:defcached
#:clear-cache
#:clear-cache-partial-arguments)
(:import-from #:website/lib/env
#:microcms-service-domain
#:microcms-api-key)
(:import-from #:website/lib/cache
#:memorize)
(:export #:get-about
#:*get-about-cache*
#:get-works
#:*get-works-cache*
#:get-blog-list
#:*get-blog-list-cache*
#:get-blog-detail
#:*get-blog-detail-cache*))
(:export #:fetch-about
#:fetch-works
#:fetch-blog-list
#:fetch-blog-detail
#:clear-about-cache
#:clear-works-cache
#:clear-blog-list-cache
#:clear-blog-detail-cache))
(in-package #:website/lib/cms)
(setf microcms:*service-domain* (microcms-service-domain))
(setf microcms:*api-key* (microcms-api-key))
(define-object-client about)
(memorize get-about)
(define-object-client works)
(memorize get-works)
(define-list-client blog)
(memorize get-blog-list)
(memorize get-blog-detail)
(defcached fetch-about (&key draft-key)
(get-about :query (list :draft-key draft-key)))
(defcached fetch-works (&key draft-key)
(get-works :query (list :draft-key draft-key)))
(defcached fetch-blog-list (&key page)
;TODO: pagenation
(declare (ignore page))
(getf (get-blog-list :query '(:fields "id,title,publishedAt"
:limit 100))
:contents))
(defcached fetch-blog-detail (id &key draft-key)
(get-blog-detail id :query (list :draft-key draft-key)))
(defun clear-about-cache (new-draft-key)
(if new-draft-key
(clear-cache-partial-arguments *fetch-about-cache* `(:draft-key ,new-draft-key))
(clear-cache *fetch-about-cache*)))
(defun clear-works-cache (new-draft-key)
(if new-draft-key
(clear-cache-partial-arguments *fetch-works-cache* `(:draft-key ,new-draft-key))
(clear-cache *fetch-works-cache*)))
(defun clear-blog-cache (id old-draft-key new-draft-key)
(labels ((clear-detail-cache (id draft-key)
(clear-cache-partial-arguments *fetch-blog-detail-cache*
`(,id :draft-key ,draft-key))))
(unless new-draft-key
(clear-cache *fetch-blog-list-cache*)
(clear-detail-cache id old-draft-key))
(clear-detail-cache id new-draft-key)))