Skip to content

Commit

Permalink
refactor: better redis utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 31, 2023
1 parent d42f7a6 commit 4ca0466
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/ua.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const redis = require('./util/redis').ua
const redis = require('./util/redis/ua')
const ua = require('@microlink/ua')(redis)

module.exports = async (req, _, next) => {
Expand Down
2 changes: 1 addition & 1 deletion src/util/keyv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const KeyvMulti = require('@keyvhq/multi')
const Keyv = require('@keyvhq/core')
const assert = require('assert')

const redis = require('./redis').cache
const redis = require('./redis')

const { CACHE_TTL } = require('../constant')

Expand Down
2 changes: 1 addition & 1 deletion src/util/rate-limiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const RateLimiter = require('async-ratelimiter')

const { RATE_LIMIT_WINDOW, RATE_LIMIT } = require('../constant')

const db = require('./redis').cache
const db = require('./redis')

module.exports = db
? new RateLimiter({
Expand Down
9 changes: 1 addition & 8 deletions src/util/redis.js → src/util/redis/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

const Redis = require('ioredis')

const { REDIS_URI, REDIS_UA_URI } = require('../constant')

const createClient = uri =>
module.exports = uri =>
uri
? new Redis(uri, {
lazyConnect: true,
enableAutoPipelining: true,
maxRetriesPerRequest: 2
})
: undefined

module.exports = {
cache: createClient(REDIS_URI),
ua: createClient(REDIS_UA_URI)
}
5 changes: 5 additions & 0 deletions src/util/redis/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

const { REDIS_URI } = require('../../constant')

module.exports = require('./create')(REDIS_URI)
5 changes: 5 additions & 0 deletions src/util/redis/ua.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

const { REDIS_UA_URI } = require('../../constant')

module.exports = require('./create')(REDIS_UA_URI)

0 comments on commit 4ca0466

Please sign in to comment.