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

caching
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 09 Nov 2022 23:05:01 -0700
parents de0cbf515ef5
children 260abd8f8565
line wrap: on
line diff
--- a/src/lib/User.luan	Tue Nov 08 14:02:28 2022 -0700
+++ b/src/lib/User.luan	Wed Nov 09 23:05:01 2022 -0700
@@ -2,6 +2,8 @@
 local error = Luan.error
 local set_metatable = Luan.set_metatable or error()
 local range = Luan.range or error()
+local set_local_only = Luan.set_local_only or error()
+local get_local_only = Luan.get_local_only or error()
 local String = require "luan:String.luan"
 local sub_string = String.sub or error()
 local Table = require "luan:Table.luan"
@@ -20,15 +22,19 @@
 
 local User = {}
 
+local users_by_name = {}
+
 local function from_doc(doc)
 	doc.type == "user" or error "wrong type"
-	return User.new {
+	local user = User.new {
 		id = doc.id
 		email = doc.user_email
 		password = doc.password
 		name = doc.user_name
 		created = doc.created
 	}
+	set_local_only(users_by_name,user.name,user)
+	return user
 end
 
 local function to_doc(user)
@@ -70,6 +76,8 @@
 end
 
 local function get_by_name(name)
+	local user = get_local_only(users_by_name,name)
+	if user ~= nil then return user end
 	local doc = Db.get_document("user_name:"..lucene_quote(name))
 	return doc and from_doc(doc)
 end