Skip to content

Commit bf1e35f

Browse files
authored
improved server caching (#5)
1 parent 20599c5 commit bf1e35f

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

source/db-server/main.js

+43-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ const databasePath = databaseFolder + 'main.db'
2222

2323
const apiPort = process.env.D3D12INFODB_CUSTOM_PORT ? process.env.D3D12INFODB_CUSTOM_PORT : 50854;
2424

25+
function GetCurrentTimeAsString() {
26+
return "" + (new Date()).valueOf()
27+
}
28+
29+
let startupTime = GetCurrentTimeAsString()
30+
let databaseModificationTime = startupTime
31+
2532
function isObjectAllowedInDB(inObj) {
2633
let isAllowed = true;
2734
database_common.submitRequiredProperites.forEach(p => {
@@ -49,12 +56,20 @@ console.log(`Opened database ${databasePath}`)
4956

5057
upgrade.upgradeDatabase(db)
5158

59+
let latestReportID = 0
60+
let latestReportQuery = db.prepare("SELECT ID FROM Submissions ORDER BY ID DESC LIMIT 1").get()
61+
if (latestReportQuery) {
62+
latestReportID = latestReportQuery.ID
63+
console.log(`Latest report ID is ${latestReportID}`)
64+
}
65+
5266
console.log("Database is ready")
5367

5468
api.use(express.json())
5569
api.use(express.urlencoded({ extended: true }))
5670
api.use(cors())
5771
api.use(compression())
72+
api.disable('etag')
5873

5974
api.get('/', (req, res) => {
6075
res.send('Server is up')
@@ -68,10 +83,25 @@ api.get('/get_submission', (req, res) => {
6883
return
6984
}
7085

86+
if (isNaN(req.query.ID)) {
87+
res.status(400)
88+
res.send('ID is not a number')
89+
return
90+
}
91+
92+
let etag = req.query.ID > latestReportID ? "0" : startupTime
93+
if (req.headers['if-none-match'] == etag) {
94+
res.status(304)
95+
res.send()
96+
return
97+
}
98+
7199
try {
72100
let rows = getSubmissionStatement.all([req.query.ID])
73101
rows = rows.map(database_common.unpackDatabaseObject)
74-
res.header("Cache-Control", "public, max-age=300")
102+
103+
res.header("Cache-Control", "public, max-age=120")
104+
res.header("ETag", etag)
75105
res.send(JSON.stringify(rows))
76106
}
77107
catch (e) {
@@ -86,10 +116,19 @@ api.get('/get_submission', (req, res) => {
86116

87117
const getAllSubmissionsStatement = db.prepare("SELECT * FROM Submissions")
88118
api.get('/get_all_submissions', (req, res) => {
119+
let etag = databaseModificationTime
120+
if (req.headers['if-none-match'] == etag) {
121+
res.status(304)
122+
res.send()
123+
return
124+
}
125+
89126
try {
127+
90128
let rows = getAllSubmissionsStatement.all()
91129
rows = rows.map(database_common.unpackDatabaseObject)
92-
res.header("Cache-Control", "public, max-age=300")
130+
res.header("Cache-Control", "public, max-age=120")
131+
res.header("ETag", databaseModificationTime)
93132
res.send(JSON.stringify(rows))
94133
}
95134
catch (e) {
@@ -166,6 +205,8 @@ api.post('/post_submission', (req, res) => {
166205
let info = postSubmissionStatement.run(parameterList)
167206

168207
res.send("" + info.lastInsertRowid)
208+
databaseModificationTime = GetCurrentTimeAsString()
209+
latestReportID = info.lastInsertRowid
169210

170211
console.log(`Inserted submission ID ${info.lastInsertRowid} - ${newSubmission["DXGI_ADAPTER_DESC3.Description"]}`)
171212

0 commit comments

Comments
 (0)