comparison 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
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 set_metatable = Luan.set_metatable or error() 3 local set_metatable = Luan.set_metatable or error()
4 local range = Luan.range or error() 4 local range = Luan.range 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 String = require "luan:String.luan" 7 local String = require "luan:String.luan"
6 local sub_string = String.sub or error() 8 local sub_string = String.sub or error()
7 local Table = require "luan:Table.luan" 9 local Table = require "luan:Table.luan"
8 local concat = Table.concat or error() 10 local concat = Table.concat or error()
9 local Math = require "luan:Math.luan" 11 local Math = require "luan:Math.luan"
18 local Db = require "site:/lib/Db.luan" 20 local Db = require "site:/lib/Db.luan"
19 21
20 22
21 local User = {} 23 local User = {}
22 24
25 local users_by_name = {}
26
23 local function from_doc(doc) 27 local function from_doc(doc)
24 doc.type == "user" or error "wrong type" 28 doc.type == "user" or error "wrong type"
25 return User.new { 29 local user = User.new {
26 id = doc.id 30 id = doc.id
27 email = doc.user_email 31 email = doc.user_email
28 password = doc.password 32 password = doc.password
29 name = doc.user_name 33 name = doc.user_name
30 created = doc.created 34 created = doc.created
31 } 35 }
36 set_local_only(users_by_name,user.name,user)
37 return user
32 end 38 end
33 39
34 local function to_doc(user) 40 local function to_doc(user)
35 local email = user.email 41 local email = user.email
36 return { 42 return {
68 local doc = Db.get_document("user_email:"..lucene_quote(email)) 74 local doc = Db.get_document("user_email:"..lucene_quote(email))
69 return doc and from_doc(doc) 75 return doc and from_doc(doc)
70 end 76 end
71 77
72 local function get_by_name(name) 78 local function get_by_name(name)
79 local user = get_local_only(users_by_name,name)
80 if user ~= nil then return user end
73 local doc = Db.get_document("user_name:"..lucene_quote(name)) 81 local doc = Db.get_document("user_name:"..lucene_quote(name))
74 return doc and from_doc(doc) 82 return doc and from_doc(doc)
75 end 83 end
76 User.get_by_name = get_by_name 84 User.get_by_name = get_by_name
77 85