lib/oleg: add cache function, use it

This commit is contained in:
Christine Dodrill 2015-02-13 11:28:23 -08:00
parent a5fa6e0124
commit 8317ad7e3b
3 changed files with 13 additions and 25 deletions

View File

@ -17,24 +17,13 @@ class Projects extends lapis.Application
@title = "Readme for #{@params.name}"
@name = @params.name
@doc, @err = oleg.get "readmes", @params.name
if @err
ngx.log ngx.NOTICE, "Need to download readme for #{@params.name}"
@doc = oleg.cache "readmes", @params.name, ->
reply, err = gh\authed_request "/repos/Xe/#{@params.name}/contents/README.md"
readme = base64.decode reply.content
if code == 404
return render: "notfound", status_code: 404
@doc = discount readme, "toc", "nopants", "autolink"
oleg.set "readmes", @params.name, @doc
@ret = readme
elseif @err
@err = "OlegDB returned code #{@err}."
return render: "error", status_code: 500
discount readme, "toc", "nopants", "autolink"
render: true

View File

@ -6,21 +6,11 @@ class Projects extends lapis.Application
[resume: "/resume"]: =>
@title = "Resume"
@doc, @err = oleg.get "resume", "cached"
if @err
ngx.log ngx.NOTICE, "Need to cache resume HTML"
@doc = oleg.cache "caches", "resume", ->
local data
with io.open "static/resume/min/resume.md", "r"
data = \read "*a"
@doc = discount data, "toc", "nopants", "autolink"
oleg.set "resume", "cached", @doc
elseif @err
@err = "OlegDB returned code #{@err}."
return render: "error", status_code: 500
discount data, "toc", "nopants", "autolink"
render: true

View File

@ -33,4 +33,13 @@ ret.delete = (tab, key) ->
ret.set = (tab, key, value) ->
request "POST", tab, key, value
ret.cache = (tab, key, getter) ->
data, err = ret.get tab, key
if err
ngx.log ngx.NOTICE, "Caching #{tab} #{key} to olegdb..."
data = getter!
ngx.log ngx.NOTICE, "done"
data
ret