change to cached_responses; actively prune table
This commit is contained in:
parent
25d782a2c5
commit
29759fe132
3 changed files with 18 additions and 11 deletions
|
|
@ -9,6 +9,8 @@ var R = require('ramda')
|
||||||
|
|
||||||
var logger = require('./logger')
|
var logger = require('./logger')
|
||||||
|
|
||||||
|
const CACHED_SESSION_TTL = 60 * 60 * 1000
|
||||||
|
|
||||||
/*
|
/*
|
||||||
function inspect(rec) {
|
function inspect(rec) {
|
||||||
console.log(require('util').inspect(rec, {depth: null, colors: true}))
|
console.log(require('util').inspect(rec, {depth: null, colors: true}))
|
||||||
|
|
@ -52,6 +54,8 @@ exports.init = function init (_conString) {
|
||||||
if (!conString) {
|
if (!conString) {
|
||||||
throw new Error('Postgres connection string is required')
|
throw new Error('Postgres connection string is required')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setInterval(pruneCachedResponses, CACHED_SESSION_TTL)
|
||||||
}
|
}
|
||||||
|
|
||||||
function connect (cb) {
|
function connect (cb) {
|
||||||
|
|
@ -778,12 +782,12 @@ function insertCachedRequest (session, path, method, body) {
|
||||||
'body'
|
'body'
|
||||||
]
|
]
|
||||||
|
|
||||||
const sql = getInsertQuery('cached_requests', fields)
|
const sql = getInsertQuery('cached_responses', fields)
|
||||||
return pquery(sql, [session.fingerprint, session.id, path, method, body])
|
return pquery(sql, [session.fingerprint, session.id, path, method, body])
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.cachedResponse = function (session, path, method) {
|
exports.cachedResponse = function (session, path, method) {
|
||||||
const sql = `select body from cached_requests
|
const sql = `select body from cached_responses
|
||||||
where device_fingerprint=$1
|
where device_fingerprint=$1
|
||||||
and session_id=$2
|
and session_id=$2
|
||||||
and path=$3
|
and path=$3
|
||||||
|
|
@ -800,8 +804,17 @@ exports.cachedResponse = function (session, path, method) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function pruneCachedResponses () {
|
||||||
|
const sql = `delete from cached_responses
|
||||||
|
where (EXTRACT(EPOCH FROM (now() - created))) * 1000 < $1`
|
||||||
|
|
||||||
|
const values = [CACHED_SESSION_TTL]
|
||||||
|
|
||||||
|
return pquery(sql, values)
|
||||||
|
}
|
||||||
|
|
||||||
exports.cacheResponse = function (session, path, method, body) {
|
exports.cacheResponse = function (session, path, method, body) {
|
||||||
const sql = `update cached_requests
|
const sql = `update cached_responses
|
||||||
set body=$1
|
set body=$1
|
||||||
where device_fingerprint=$2
|
where device_fingerprint=$2
|
||||||
and session_id=$3
|
and session_id=$3
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,14 @@
|
||||||
var db = require('./db')
|
var db = require('./db')
|
||||||
|
|
||||||
exports.up = function (next) {
|
exports.up = function (next) {
|
||||||
db.query('CREATE TABLE IF NOT EXISTS cached_requests ( ' +
|
db.query('CREATE TABLE IF NOT EXISTS cached_responses ( ' +
|
||||||
'id serial PRIMARY KEY, ' +
|
'id serial PRIMARY KEY, ' +
|
||||||
'device_fingerprint text NOT NULL, ' +
|
'device_fingerprint text NOT NULL, ' +
|
||||||
'session_id uuid NOT NULL, ' +
|
'session_id uuid NOT NULL, ' +
|
||||||
'path text NOT NULL, ' +
|
'path text NOT NULL, ' +
|
||||||
'method text NOT NULL, ' +
|
'method text NOT NULL, ' +
|
||||||
'body json NOT NULL, ' +
|
'body json NOT NULL, ' +
|
||||||
|
'created timestampz NOT NULL DEFAULT now(), ' +
|
||||||
'UNIQUE (device_fingerprint, session_id, path, method) ' +
|
'UNIQUE (device_fingerprint, session_id, path, method) ' +
|
||||||
')', next)
|
')', next)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
7
todo.txt
7
todo.txt
|
|
@ -1,8 +1 @@
|
||||||
- change satoshis to crypto_atoms in db (ask neal about this)
|
- change satoshis to crypto_atoms in db (ask neal about this)
|
||||||
|
|
||||||
UTC:
|
|
||||||
http://justatheory.com/computers/databases/postgresql/use-timestamptz.html
|
|
||||||
|
|
||||||
Upgrade instructions:
|
|
||||||
|
|
||||||
AFTER migrating, set timezone in config to 'UTC', then restart server
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue