comparison 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
comparison
equal deleted inserted replaced
42:0c1b820fff34 43:298c71e0c854
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local ipairs = Luan.ipairs or error() 3 local ipairs = Luan.ipairs or error()
4 local set_metatable = Luan.set_metatable or error() 4 local set_metatable = Luan.set_metatable or error()
5 local set_local_only = Luan.set_local_only or error()
6 local get_local_only = Luan.get_local_only or error()
5 local Time = require "luan:Time.luan" 7 local Time = require "luan:Time.luan"
6 local time_now = Time.now or error() 8 local time_now = Time.now or error()
7 local Html = require "luan:Html.luan" 9 local Html = require "luan:Html.luan"
8 local html_encode = Html.encode or error() 10 local html_encode = Html.encode or error()
9 local Number = require "luan:Number.luan" 11 local Number = require "luan:Number.luan"
34 time = 1000*60 36 time = 1000*60
35 unit = "minute" 37 unit = "minute"
36 } 38 }
37 } 39 }
38 40
41 local posts_by_id = {}
42
39 local function from_doc(doc) 43 local function from_doc(doc)
40 doc.type == "post" or error "wrong type" 44 doc.type == "post" or error "wrong type"
41 return Post.new { 45 local post = Post.new {
42 id = doc.id 46 id = doc.id
43 content = doc.content 47 content = doc.content
44 date = doc.date 48 date = doc.date
45 author_name = doc.post_author_name 49 author_name = doc.post_author_name
46 root_id = doc.post_root_id 50 root_id = doc.post_root_id
47 is_root = doc.post_is_root == "true" 51 is_root = doc.post_is_root == "true"
48 52
49 -- root only 53 -- root only
50 subject = doc.subject 54 subject = doc.subject
51 } 55 }
56 set_local_only(posts_by_id,post.id,post)
57 return post
52 end 58 end
53 59
54 local function to_doc(post) 60 local function to_doc(post)
55 return { 61 return {
56 type = "post" 62 type = "post"
121 set_metatable(post,metatable) 127 set_metatable(post,metatable)
122 return post 128 return post
123 end 129 end
124 130
125 function Post.get_by_id(id) 131 function Post.get_by_id(id)
132 local post = get_local_only(posts_by_id,id)
133 if post ~= nil then return post end
126 local doc = Db.get_document("id:"..id) 134 local doc = Db.get_document("id:"..id)
127 return doc and from_doc(doc) 135 return doc and from_doc(doc)
128 end 136 end
129 137
130 function Post.new_thread(author,subject,content) 138 function Post.new_thread(author,subject,content)