-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (92 loc) · 3.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const mongoose = require('mongoose')
const Q = require('q')
const CachePage = require('./models/CachePage')
const CachePageList = require('./models/CachePageList')
const CacheTagList = require('./models/CacheTagList')
const CacheProfile = require('./models/CacheProfile')
const CacheGroup = require('./models/CacheGroup')
const CacheGroupList = require('./models/CacheGroupList')
const collections = {
'page': CachePage,
'pages': CachePageList,
'tags': CacheTagList,
'user' : CacheProfile,
'group' : CacheGroup,
'groups' : CacheGroupList
}
let isDebug
let isDisabled
function start({
mongoDsn,
debug,
disabled
}) {
isDebug = debug
isDisabled = disabled
//mongoose.set('debug', debug)
const options = {
autoIndex: false, // Don't build indexes
useNewUrlParser: true,
poolSize: 20, // Maintain up to 10 socket connection
bufferMaxEntries: 0,
connectTimeoutMS: 200000, // Give up initial connection after 10 seconds
socketTimeoutMS: 300000, // Close sockets after 45 seconds of inactivity
family: 4,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
}
setTimeout(function () {
const mongoDB = mongoose.connect(mongoDsn, options)
//.then(() => console.log('connected to mongo'))
.catch((err) => console.log('error connecting to mongo\n\r' + err))
})
}
function exists(type, websiteId, params) {
if (!collections[type]) throw new Error(`[gc cache] model for ${type} not found`)
return collections[type].exists(websiteId, params)
}
function update(type, websiteId, params, content) {
if (!collections[type]) throw new Error(`[gc cache] model for ${type} not found`)
return collections[type].updateOrCreate(websiteId, params, content)
}
function remove(type) {
if (!collections[type]) throw new Error(`[gc cache] model for ${type} not found`)
return collections[type].deleteAll().then(
result => console.log(`[cache ${type}] ${result.deletedCount} results removed`),
err => console.log(`[cache ${type}] error`, err),
)
}
function clear(cb) {
return Promise.all(
Object.keys(collections).map(remove)
).then(cb)
}
function get(type, websiteId, params, res, generate) {
exists(type, websiteId, params).then(
cached => {
if (isDisabled) {
generate().then(content => res.send(content))
}
else if (cached && cached.valid) {
if (isDebug) console.log(`[cache ${type}] found ${JSON.stringify(params)}`)
res.append('Last-Modified', (new Date(cached.updated_at)).toUTCString())
res.send(cached.content)
}
else {
if (isDebug) console.log(`[cache ${type}] generate ${JSON.stringify(params)}`)
generate().then(content => {
if (content.indexOf('data-ignore-caching') === -1) {
update(type, websiteId, params, content).catch(err => console.error('[gc cache]', err))
}
res.send(content)
})
}
},
err => console.error('[gc cache]', err)
)
}
function exit() {
mongoose.disconnect()
}
module.exports = { start, exists, update, get, remove, clear, exit }