Fix cacheing

Fix some cache invalidation bugs that would cause the index and author
pages to not update correctly when new pastes were made.
This commit is contained in:
Robin Malley 2020-05-19 20:11:11 -04:00
parent 910542ef76
commit 2e735282ec
1 changed files with 35 additions and 27 deletions

View File

@ -1,7 +1,9 @@
local et = require("etlua") local et = require("etlua")
local sql = require("lsqlite3") local sql = require("lsqlite3")
local zlib = require("zlib") local zlib = require("zlib")
if PRODUCTION then
local function print() end --squash prints local function print() end --squash prints
end
local parser_names = {"plain","imageboard"} local parser_names = {"plain","imageboard"}
local parsers = {} local parsers = {}
for _,v in pairs(parser_names) do for _,v in pairs(parser_names) do
@ -231,7 +233,7 @@ end
local function dirty_cache(url) local function dirty_cache(url)
stmnt_dirty_cache:bind_names{ stmnt_dirty_cache:bind_names{
path = string.format("%s/%s",domain,url) path = url
} }
err = do_sql(stmnt_dirty_cache) err = do_sql(stmnt_dirty_cache)
stmnt_dirty_cache:reset() stmnt_dirty_cache:reset()
@ -330,7 +332,7 @@ function home(req)
local text local text
if host == domain then if host == domain then
--Default home page --Default home page
text = render(host..path,function() text = render(string.format("%s",domain),function()
print("Cache miss, rendering index") print("Cache miss, rendering index")
stmnt_index:bind_names{} stmnt_index:bind_names{}
local err = do_sql(stmnt_index) local err = do_sql(stmnt_index)
@ -417,7 +419,7 @@ function claim(req)
local text local text
if method == "GET" then if method == "GET" then
--Get the page to claim a name --Get the page to claim a name
text = render(host..path,function() text = render(string.format("%s/_claim",domain),function()
print("cache miss, rendering claim page") print("cache miss, rendering claim page")
return pages.claim{} return pages.claim{}
end) end)
@ -474,10 +476,11 @@ function paste(req)
--Get the paste page --Get the paste page
if host == domain then if host == domain then
--For an anonymous user --For an anonymous user
ret = render(host..path,function() ret = render(string.format("%s/_paste",host),function()
print("Cache missing, rendering post page") print("Cache missing, rendering post page")
return pages.paste{ return pages.paste{
domain = domain, domain = domain,
err = "",
} }
end) end)
else else
@ -508,6 +511,7 @@ function paste(req)
domain = domain, domain = domain,
user = author, user = author,
text = "", text = "",
err = "",
} }
end end
elseif method == "POST" then elseif method == "POST" then
@ -615,6 +619,7 @@ function paste(req)
http_response(req,303,"") http_response(req,303,"")
stmnt_paste:reset() stmnt_paste:reset()
stmnt_raw:reset() stmnt_raw:reset()
dirty_cache(string.format("%s.%s",author,domain))
dirty_cache(string.format("%s/%s",domain,url)) dirty_cache(string.format("%s/%s",domain,url))
dirty_cache(string.format("%s",domain)) dirty_cache(string.format("%s",domain))
return return
@ -632,7 +637,7 @@ end
--A helper function for below --A helper function for below
local function read_story(host,path,idp) local function read_story(host,path,idp)
return render(host..path,function() return render(string.format("%s%s",host,path),function()
print("Trying to read, id is",idp,":",decode_id(idp)) print("Trying to read, id is",idp,":",decode_id(idp))
local id = decode_id(idp) local id = decode_id(idp)
print("id:",id,type(id)) print("id:",id,type(id))
@ -679,15 +684,16 @@ function read(req)
if err == sql.DONE then if err == sql.DONE then
--We got no story --We got no story
stmnt_read:reset() stmnt_read:reset()
return pages.nostory{ text = pages.nostory{
path = path path = path
} }
end else
assert(err == sql.ROW) assert(err == sql.ROW)
local title, storytext, tauthor, isanon, authorname = unpack(stmnt_read:get_values()) local title, storytext, tauthor, isanon, authorname = unpack(stmnt_read:get_values())
storytext = zlib.decompress(storytext) storytext = zlib.decompress(storytext)
stmnt_read:reset() stmnt_read:reset()
if tauthor == authorid then if tauthor == authorid then
print("we're the author!")
--The story exists and we're logged in as the --The story exists and we're logged in as the
--owner, display the edit button --owner, display the edit button
text = pages.read{ text = pages.read{
@ -701,8 +707,10 @@ function read(req)
} }
else else
print("we're not the author!")
text = read_story(host,path,idp) text = read_story(host,path,idp)
end end
end
else else
text = read_story(host,path,idp) text = read_story(host,path,idp)
end end
@ -723,7 +731,7 @@ function login(req)
local text local text
if method == "GET" then if method == "GET" then
--Just give them the login page --Just give them the login page
text = render(host..path,function() text = render(string.format("%s/_login",domain),function()
return pages.login{ return pages.login{
err = "", err = "",
} }