Skip to content

Commit

Permalink
Merge pull request #173 from ekamahuja/private-ecma-#
Browse files Browse the repository at this point in the history
Refactor: Replace private keyword with ECMAScript private fields
  • Loading branch information
ilovepixelart committed Feb 25, 2024
2 parents 9d7989f + bc82eb9 commit e846475
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
26 changes: 13 additions & 13 deletions src/cache/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,49 @@ import MemoryCacheEngine from './engine/MemoryCacheEngine'
import RedisCacheEngine from './engine/RedisCacheEngine'

class CacheEngine {
private engine!: ICacheEngine
private defaultTTL: number
private readonly engines = ['memory', 'redis'] as const
#engine!: ICacheEngine
#defaultTTL: number
readonly #engines = ['memory', 'redis'] as const

constructor (cacheOptions: ICacheOptions) {
if (!this.engines.includes(cacheOptions.engine)) {
if (!this.#engines.includes(cacheOptions.engine)) {
throw new Error(`Invalid engine name: ${cacheOptions.engine}`)
}

if (cacheOptions.engine === 'redis' && !cacheOptions.engineOptions) {
throw new Error(`Engine options are required for ${cacheOptions.engine} engine`)
}

this.defaultTTL = ms(cacheOptions.defaultTTL ?? '1 minute')
this.#defaultTTL = ms(cacheOptions.defaultTTL ?? '1 minute')

if (cacheOptions.engine === 'redis' && cacheOptions.engineOptions) {
this.engine = new RedisCacheEngine(cacheOptions.engineOptions)
this.#engine = new RedisCacheEngine(cacheOptions.engineOptions)
}

if (cacheOptions.engine === 'memory') {
this.engine = new MemoryCacheEngine()
this.#engine = new MemoryCacheEngine()
}
}

async get (key: string): Promise<IData> {
return this.engine.get(key)
return this.#engine.get(key)
}

async set (key: string, value: Record<string, unknown> | Record<string, unknown>[], ttl: string | null): Promise<void> {
const actualTTL = ttl ? ms(ttl) : this.defaultTTL
return this.engine.set(key, value, actualTTL)
const actualTTL = ttl ? ms(ttl) : this.#defaultTTL
return this.#engine.set(key, value, actualTTL)
}

async del (key: string): Promise<void> {
return this.engine.del(key)
return this.#engine.del(key)
}

async clear (): Promise<void> {
return this.engine.clear()
return this.#engine.clear()
}

async close (): Promise<void> {
return this.engine.close()
return this.#engine.close()
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/cache/engine/MemoryCacheEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import type IData from '../../interfaces/IData'
import type ICacheEngine from '../../interfaces/ICacheEngine'

class MemoryCacheEngine implements ICacheEngine {
private cache: Map<string, { value: IData, expiresAt: number } | undefined>
#cache: Map<string, { value: IData, expiresAt: number } | undefined>

constructor () {
this.cache = new Map()
this.#cache = new Map()
}

get (key: string): IData {
const item = this.cache.get(key)
const item = this.#cache.get(key)
if (!item || item.expiresAt < Date.now()) {
this.del(key)
return undefined
Expand All @@ -18,18 +18,18 @@ class MemoryCacheEngine implements ICacheEngine {
}

set (key: string, value: IData, ttl = Infinity): void {
this.cache.set(key, {
this.#cache.set(key, {
value,
expiresAt: Date.now() + ttl
})
}

del (key: string): void {
this.cache.delete(key)
this.#cache.delete(key)
}

clear (): void {
this.cache.clear()
this.#cache.clear()
}

close (): void {
Expand Down
14 changes: 7 additions & 7 deletions src/cache/engine/RedisCacheEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import type IData from '../../interfaces/IData'
import type ICacheEngine from '../../interfaces/ICacheEngine'

class RedisCacheEngine implements ICacheEngine {
private client: Redis
#client: Redis

constructor (options: RedisOptions) {
if (!options.keyPrefix) {
options.keyPrefix = 'cache-mongoose:'
}
this.client = new IORedis(options)
this.#client = new IORedis(options)
}

async get (key: string): Promise<IData> {
const value = await this.client.get(key)
const value = await this.#client.get(key)
if (value === null) {
return undefined
}
Expand All @@ -35,19 +35,19 @@ class RedisCacheEngine implements ICacheEngine {

async set (key: string, value: unknown, ttl = Infinity): Promise<void> {
const serializedValue = JSON.stringify(value)
await this.client.setex(key, Math.ceil(ttl / 1000), serializedValue)
await this.#client.setex(key, Math.ceil(ttl / 1000), serializedValue)
}

async del (key: string): Promise<void> {
await this.client.del(key)
await this.#client.del(key)
}

async clear (): Promise<void> {
await this.client.flushdb()
await this.#client.flushdb()
}

async close (): Promise<void> {
await this.client.quit()
await this.#client.quit()
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,25 @@ declare module 'mongoose' {
}

class CacheMongoose {
// eslint-disable-next-line no-use-before-define
private static instance: CacheMongoose | undefined
static #instance: CacheMongoose | undefined
private cache!: Cache

private constructor () {
// Private constructor to prevent external instantiation
}

public static init (mongoose: Mongoose, cacheOptions: ICacheOptions): CacheMongoose {
if (!this.instance) {
this.instance = new CacheMongoose()
this.instance.cache = new Cache(cacheOptions)
if (!this.#instance) {
this.#instance = new CacheMongoose()
this.#instance.cache = new Cache(cacheOptions)

const cache = this.instance.cache
const cache = this.#instance.cache

extendQuery(mongoose, cache)
extendAggregate(mongoose, cache)
}

return this.instance
return this.#instance
}

public async clear (customKey?: string): Promise<void> {
Expand Down

0 comments on commit e846475

Please sign in to comment.