-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (63 loc) · 2.12 KB
/
index.js
File metadata and controls
67 lines (63 loc) · 2.12 KB
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
const RedisStore = require('./lib/stores/redis-store')
const MemoryStore = require('./lib/stores/memory-store')
const SQLiteStore = require('./lib/stores/sqlite-store')
module.exports = function defineSailsCacheHook(sails) {
return {
defaults: {
stash: {
cachestore: 'default',
},
cachestores: {
default: {
store: 'memory',
},
},
},
initialize: async function () {
function getCacheStore(cachestore) {
if (!sails.config.cachestores[cachestore]) {
throw new Error('The provided cachestore coult not be found.')
}
switch (sails.config.cachestores[cachestore].store) {
case 'memory':
return new MemoryStore(sails)
case 'redis':
return new RedisStore(sails)
case 'sqlite':
return new SQLiteStore(sails)
default:
throw new Error(
'Invalid store provided, supported stores are memory, redis, and sqlite.',
)
}
}
let cacheStore = getCacheStore(sails.config.stash.cachestore)
if (
sails.config.cachestores[sails.config.stash.cachestore].store ===
'memory' &&
sails.config.environment === 'production'
) {
sails.log.warn(
'Sails Stash is using the memory store in production. ' +
'This is not recommended for production environments. ' +
'Consider switching to a persistent cache store. ' +
'See: https://docs.sailscasts.com/sails-stash/redis',
)
}
sails.cache = {
get: cacheStore.get.bind(cacheStore),
set: cacheStore.set.bind(cacheStore),
has: cacheStore.has.bind(cacheStore),
delete: cacheStore.delete.bind(cacheStore),
fetch: cacheStore.fetch.bind(cacheStore),
add: cacheStore.add.bind(cacheStore),
pull: cacheStore.pull.bind(cacheStore),
forever: cacheStore.forever.bind(cacheStore),
destroy: cacheStore.destroy.bind(cacheStore),
store: function (cachestore) {
return getCacheStore(cachestore)
},
}
},
}
}