Skip to content
This repository was archived by the owner on Nov 10, 2021. It is now read-only.

Commit 2b22d91

Browse files
committed
Implement edge cache for feed
1 parent e8aef9e commit 2b22d91

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

conseilUtil.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ const getArtisticOutputForAddress = async (address) => {
136136
return objectInfo
137137
}
138138

139-
const getArtisticUniverse = async (offset) => {
139+
const getArtisticUniverse = async (max_time) => {
140140
let mintOperationQuery = conseiljs.ConseilQueryBuilder.blankQuery();
141141
mintOperationQuery = conseiljs.ConseilQueryBuilder.addFields(mintOperationQuery, 'operation_group_hash');
142142
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'kind', conseiljs.ConseilOperator.EQ, ['transaction'])
143143
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'timestamp', conseiljs.ConseilOperator.AFTER, [1612240919000]) // 2021 Feb 1
144+
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'timestamp', conseiljs.ConseilOperator.BEFORE, [max_time]) // quantized time (1min)
144145
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'status', conseiljs.ConseilOperator.EQ, ['applied'])
145146
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'destination', conseiljs.ConseilOperator.EQ, [mainnet.protocol])
146147
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'parameters_entrypoints', conseiljs.ConseilOperator.EQ, ['mint_OBJKT'])

index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,18 @@ const filterTz = (data, tz) => _.filter(data, { tz: tz })
127127

128128
const test = async () => console.log(desc(await getObjkts()))
129129

130-
const getFeed = async (counter, res) => {
131-
var arr = await conseilUtil.getArtisticUniverse(0)
130+
const customFloor = function(value, roundTo) {
131+
return Math.floor(value / roundTo) * roundTo;
132+
}
133+
134+
const ONE_MINUTE_MILLIS = 60 * 1000
135+
136+
137+
const getFeed = async (counter, max_time, res) => {
138+
const now_time = Date.now()
139+
const immutable = (typeof max_time !== 'undefined') && (max_time < now_time)
140+
max_time = (typeof max_time !== 'undefined') ? max_time : customFloor(now_time, ONE_MINUTE_MILLIS)
141+
var arr = await conseilUtil.getArtisticUniverse(max_time)
132142
var feed = offset(desc(arr), counter)
133143
console.log(feed)
134144
feed = await feed.map(async e => {
@@ -138,11 +148,18 @@ const getFeed = async (counter, res) => {
138148
return e
139149
})
140150
//console.log(feed)
141-
151+
var cache_time
152+
if (immutable) {
153+
cache_time = 60*10
154+
}
155+
else {
156+
cache_time = (int)(((max_time + ONE_MINUTE_MILLIS) - now_time) / 1000)
157+
}
142158
var promise = Promise.all(feed.map(e => e))
143159
promise.then(async (results) => {
144160
var aux_arr = results.map(e => e)
145-
161+
162+
res.set('Cache-Control', `public, max-age=${cache_time}`)
146163
//console.log(aux_arr)
147164
res.json({ result: aux_arr })
148165
})
@@ -247,8 +264,10 @@ const app = express()
247264
app.use(express.json())
248265
app.use(cors({ origin: '*' }))
249266

250-
app.post('/feed', async (req, res) => {
251-
await getFeed(req.body.counter, res)
267+
app.get('/feed', async (req, res) => {
268+
var counter = req.query.counter
269+
var max_time = req.query.hasOwnProperty('time') ? customFloor(req.query.time, ONE_MINUTE_MILLIS) : null
270+
await getFeed(counter, max_time, res)
252271
})
253272

254273
app.post('/tz', async (req, res) => {

0 commit comments

Comments
 (0)