Optimize post/edit/remove speed

parent 364f2c26
......@@ -114,7 +114,7 @@
("comment_status" . "closed")))
(oid (git-post-article url-name article-content
article-meta)))
(update-index-posts-cache)
(update-index-posts-cache oid 'post)
oid)))
(post "/v1/colt/edit_article"
......@@ -133,7 +133,7 @@
(oid (git-edit-article article-title article-name article-content
article-oid article-tags article-status
update-timestamp?)))
(update-index-posts-cache)
(update-index-posts-cache oid 'edit)
oid)))
(post "/v1/colt/remove_article"
......@@ -147,7 +147,7 @@
(article-oid (hash-ref json-content "oid"))
(status (git-remove-article article-title article-content
article-oid)))
(update-index-posts-cache)
(update-index-posts-cache article-oid 'remove)
(if (string=? status "failed")
"failed"
"ok"))))
......
;; -*- indent-tabs-mode:nil; coding: utf-8 -*-
;; Copyright (C) 2017,2019
;; Copyright (C) 2017,2019,2020
;; "Mu Lei" known as "NalaGinrut" <NalaGinrut@gmail.com>
;; Colt is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License
......@@ -64,41 +64,43 @@
(define *intro* '())
(define *all-tags* (make-hash-table))
(define (refresh-tags)
(for-each
(lambda (p)
(for-each
(lambda (t)
(let ((pl (hash-ref *all-tags* t '())))
(hash-set! *all-tags* t (cons p pl))))
(tags-string->list (post-tags p))))
*all-post-objs*))
(define (sort-by-time objs)
(sort objs (lambda (x y)
(>= (string->number (post-timestamp x))
(string->number (post-timestamp y))))))
(define* (get-all-post-objs #:key (latest-top? #t) (refresh? #f))
(define (sort-by-time objs)
(sort objs (lambda (x y)
(>= (string->number (post-timestamp (get-post x)))
(string->number (post-timestamp (get-post y)))))))
(define return (if latest-top? identity reverse))
(define (filter-me objs)
(partition (lambda (o)
(string=? "_____colt_____Intro" (post-title (get-post o))))
objs))
(define (filter-me posts)
(partition (lambda (p)
(string=? "_____colt_____Intro" (post-title p)))
posts))
(when refresh? (set! *all-post-objs* '()))
(cond
((not (null? *all-post-objs*))
(return *all-post-objs*))
(else
(let ((objs (sort-by-time (git/get-post-objs))))
(let ((posts (sort-by-time (git/get-post-objs))))
(call-with-values
(lambda () (filter-me objs))
(lambda () (filter-me posts))
(lambda (intro all)
(set! *all-post-objs* all)
(set! *intro* (map get-post intro))
;; init tags
(for-each
(lambda (o)
(for-each
(lambda (t)
(let ((pl (hash-ref *all-tags* t '())))
(hash-set! *all-tags* t (cons (get-post o) pl))))
(tags-string->list (post-tags (get-post o)))))
all)))
(set! *intro* intro)
(refresh-tags)))
(return *all-post-objs*)))))
(define* (get-all-posts #:key (latest-top? #t) (refresh? #f))
(map get-post (get-all-post-objs #:latest-top? latest-top? #:refresh? refresh?)))
(get-all-post-objs #:latest-top? latest-top? #:refresh? refresh?))
(define (get-posts-by-tag tag)
(hash-ref *all-tags* (uri-decode tag) '()))
......@@ -128,10 +130,43 @@
(define (get-the-latest-post-update-time)
(post-timestamp (get-the-latest-post)))
(define (update-index-posts-cache)
(define (get-one-post-from-git oid)
(let ((ol (git-ls-tree)))
(get-post-by-oid oid ol)))
(define (cache-the-post! oid op)
(let ((new-post (get-one-post-from-git oid)))
(cond
((eq? op 'post)
(set! *all-post-objs*
(sort-by-time
(cons new-post *all-post-objs*))))
((eq? op 'edit)
(let ((url-name (uri-decode (post-url-name new-post))))
(set! *all-post-objs*
(sort-by-time
(cons new-post
(filter (lambda (p)
(not (string=? url-name
(uri-decode (post-url-name p)))))
*all-post-objs*)))))
(refresh-tags))
((eq? op 'remove)
(set! *all-post-objs*
(filter (lambda (p)
(not (string=? oid (post-oid p))))
*all-post-objs*))
(refresh-tags))
(else
(throw 'artanis-err 500 cache-the-post!
"Invalid operation or oid: ~a, ~a" op oid)))))
(define* (update-index-posts-cache #:optional (oid #f) (op 'post))
(clear-content-cache "index")
(clear-content-cache "atom/feed")
(get-all-posts #:refresh? #t))
(clear-content-cache "feed/atom")
(if oid
(cache-the-post! oid op)
(get-all-posts #:refresh? #t)))
(define (get-posts-from-n-to-m n m posts-list)
(if (null? posts-list)
......@@ -260,7 +295,7 @@
(generate-pages-content mode (get-intro-page)))
(define (get-one-article-by-oid oid)
(let ((post (get-post-by-oid oid (get-all-post-objs))))
(let ((post (get-post-in-pl-by-oid oid (get-all-post-objs))))
(when (not post)
(throw 'artanis-err 404 get-one-article-by-oid
"Invalid oid (~a)!" oid))
......
......@@ -12,7 +12,7 @@
<@css materialize.min.css %>
<@css blog.css %>
<@css custom-blue-theme.css %>
<@js jquery-3.3.1.min.js %>
<@js jquery-3.4.1.min.js %>
<@js materialize.min.js %>
<script>
$("#reset").on("click", function() {
......
......@@ -33,7 +33,9 @@
get-object-by-oid
get-object-by-name
get-post-by-oid
get-post-in-pl-by-oid
get-post
git-object-oid
post-meta-data
post-content
......@@ -157,6 +159,9 @@
(define (get-post-by-oid oid obj-list)
(any (lambda (o) (and (string=? oid (git-object-oid o)) (get-post o))) obj-list))
(define (get-post-in-pl-by-oid oid post-list)
(any (lambda (p) (and (string=? oid (post-oid p)) p)) post-list))
(define* (git-ls-tree #:key (branch "master"))
(cmd-result-map
(lambda (o)
......@@ -350,12 +355,12 @@
(make-post meta-data content comments url-name oid)))
(define (git/get-post-objs)
(let ((ol (git-ls-tree)))
(let ((pl (map get-post (git-ls-tree))))
(filter
(lambda (o)
(not (string=? (meta-data-status (post-meta-data (get-post o)))
(lambda (p)
(not (string=? (meta-data-status (post-meta-data p))
"deleted")))
ol)))
pl)))
(define (git-db-init)
(cmd git init --bare --quiet)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment