2015-02-07 00:17:05 +00:00
|
|
|
config = require("lapis.config").get!
|
|
|
|
http = require "lapis.nginx.http"
|
|
|
|
|
2015-02-07 04:30:46 +00:00
|
|
|
request = (method, table, key, value=nil, headers={}) ->
|
2015-02-07 00:17:05 +00:00
|
|
|
oleg_res, code = http.simple {
|
2015-02-07 04:30:46 +00:00
|
|
|
url: "http://#{config.oleg.host}:#{config.oleg.port}/#{table}/#{key}"
|
|
|
|
method: method
|
|
|
|
body: value
|
|
|
|
headers: headers
|
2015-02-07 00:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if code ~= 200
|
|
|
|
return nil, code
|
|
|
|
|
|
|
|
if not oleg_res
|
|
|
|
return nil, 404
|
|
|
|
|
|
|
|
oleg_res, nil
|
|
|
|
|
|
|
|
ret = {}
|
|
|
|
|
2015-02-07 04:30:46 +00:00
|
|
|
ret.request = request
|
2015-02-07 00:17:05 +00:00
|
|
|
|
2015-02-07 04:30:46 +00:00
|
|
|
--- get gets a key from a table and returns the data and either nil or a error message
|
|
|
|
ret.get = (tab, key) ->
|
|
|
|
request "GET", tab, key
|
2015-02-07 00:17:05 +00:00
|
|
|
|
2015-02-07 04:30:46 +00:00
|
|
|
--- delete deltes key from tab
|
|
|
|
ret.delete = (tab, key) ->
|
|
|
|
request "DELETE", tab, key
|
|
|
|
|
|
|
|
--- set sets key in tab to value
|
|
|
|
ret.set = (tab, key, value) ->
|
|
|
|
request "POST", tab, key, value
|
2015-02-07 00:17:05 +00:00
|
|
|
|
2015-02-13 19:34:05 +00:00
|
|
|
--- cache gets a key from tab, optionally running the getter function if key is not in tab
|
2015-02-13 19:28:23 +00:00
|
|
|
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
|
|
|
|
|
2015-02-07 00:17:05 +00:00
|
|
|
ret
|