diff src/lib/Post.luan @ 43:298c71e0c854

caching
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 09 Nov 2022 23:05:01 -0700
parents 0c1b820fff34
children 289718f121e4
line wrap: on
line diff
--- a/src/lib/Post.luan	Tue Nov 08 14:02:28 2022 -0700
+++ b/src/lib/Post.luan	Wed Nov 09 23:05:01 2022 -0700
@@ -2,6 +2,8 @@
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
 local set_metatable = Luan.set_metatable or error()
+local set_local_only = Luan.set_local_only or error()
+local get_local_only = Luan.get_local_only or error()
 local Time = require "luan:Time.luan"
 local time_now = Time.now or error()
 local Html = require "luan:Html.luan"
@@ -36,9 +38,11 @@
 	}
 }
 
+local posts_by_id = {}
+
 local function from_doc(doc)
 	doc.type == "post" or error "wrong type"
-	return Post.new {
+	local post = Post.new {
 		id = doc.id
 		content = doc.content
 		date = doc.date
@@ -49,6 +53,8 @@
 		-- root only
 		subject = doc.subject
 	}
+	set_local_only(posts_by_id,post.id,post)
+	return post
 end
 
 local function to_doc(post)
@@ -123,6 +129,8 @@
 end
 
 function Post.get_by_id(id)
+	local post = get_local_only(posts_by_id,id)
+	if post ~= nil then return post end
 	local doc = Db.get_document("id:"..id)
 	return doc and from_doc(doc)
 end