From 8317ad7e3ba8bb82774ecbbca784e49376995d7b Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 13 Feb 2015 11:28:23 -0800 Subject: [PATCH] lib/oleg: add cache function, use it --- controllers/projects.moon | 15 ++------------- controllers/resume.moon | 14 ++------------ lib/oleg.moon | 9 +++++++++ 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/controllers/projects.moon b/controllers/projects.moon index 93f9ff0..d9b89ff 100644 --- a/controllers/projects.moon +++ b/controllers/projects.moon @@ -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 diff --git a/controllers/resume.moon b/controllers/resume.moon index d042dfa..85210b8 100644 --- a/controllers/resume.moon +++ b/controllers/resume.moon @@ -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 diff --git a/lib/oleg.moon b/lib/oleg.moon index bd5290f..e9c0b1e 100644 --- a/lib/oleg.moon +++ b/lib/oleg.moon @@ -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