-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
32 lines (24 loc) · 759 Bytes
/
Copy pathcache.js
File metadata and controls
32 lines (24 loc) · 759 Bytes
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
const createClient = require('redis').createClient
// cache class
class Cache {
// create client and check for any connection error
constructor(){
const redisURL = process.env.REDIS_URL || 'redis://localhost:6379'
this.client = createClient({url: redisURL})
this.client.on('err', (err) => console.log('Redis client error: ', err))
}
// returns connection method
connect(){
return this.client.connect()
}
// set cache
async set(key, value){
await this.client.set(key, JSON.stringify(value))
}
// get cache if hit otherwise return null
async get(key){
const value = await this.client.get(key)
return JSON.parse(value)
}
}
module.exports = Cache