Skip to content

Commit 8851b9a

Browse files
committed
Move all types to one file and renamed:
- type IData -> CacheData - type ICacheOptions -> CacheOptions - type ICacheEngine -> CacheEngine - class CacheEngine -> Cache
1 parent 8d56584 commit 8851b9a

File tree

12 files changed

+61
-74
lines changed

12 files changed

+61
-74
lines changed

src/cache/Cache.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ import ms from 'ms'
33
import MemoryCacheEngine from './engine/MemoryCacheEngine'
44
import RedisCacheEngine from './engine/RedisCacheEngine'
55

6-
import type { StringValue } from 'ms'
7-
import type ICacheEngine from '../interfaces/ICacheEngine'
8-
import type ICacheOptions from '../interfaces/ICacheOptions'
9-
import type IData from '../interfaces/IData'
10-
11-
class CacheEngine {
12-
#engine!: ICacheEngine
13-
#defaultTTL: number
14-
#debug: boolean
6+
import type { CacheData, CacheEngine, CacheOptions, CacheTTL } from '../types'
7+
8+
class Cache {
9+
readonly #engine!: CacheEngine
10+
readonly #defaultTTL: number
11+
readonly #debug: boolean
1512
readonly #engines = ['memory', 'redis'] as const
1613

17-
constructor(cacheOptions: ICacheOptions) {
14+
constructor(cacheOptions: CacheOptions) {
1815
if (!this.#engines.includes(cacheOptions.engine)) {
1916
throw new Error(`Invalid engine name: ${cacheOptions.engine}`)
2017
}
@@ -40,7 +37,7 @@ class CacheEngine {
4037
this.#debug = cacheOptions.debug === true
4138
}
4239

43-
async get(key: string): Promise<IData> {
40+
async get(key: string): Promise<CacheData> {
4441
const cacheEntry = await this.#engine.get(key)
4542
if (this.#debug) {
4643
const cacheHit = cacheEntry != null ? 'HIT' : 'MISS'
@@ -49,7 +46,7 @@ class CacheEngine {
4946
return cacheEntry
5047
}
5148

52-
async set(key: string, value: IData, ttl: number | StringValue | null): Promise<void> {
49+
async set(key: string, value: CacheData, ttl: CacheTTL | null): Promise<void> {
5350
const givenTTL = typeof ttl === 'string' ? ms(ttl) : ttl
5451
const actualTTL = givenTTL ?? this.#defaultTTL
5552
await this.#engine.set(key, value, actualTTL)
@@ -77,4 +74,4 @@ class CacheEngine {
7774
}
7875
}
7976

80-
export default CacheEngine
77+
export default Cache

src/cache/engine/MemoryCacheEngine.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import ms from 'ms'
22

3-
import type { StringValue } from 'ms'
4-
import type ICacheEngine from '../../interfaces/ICacheEngine'
5-
import type IData from '../../interfaces/IData'
3+
import type { CacheData, CacheEngine, CacheTTL } from '../../types'
64

7-
class MemoryCacheEngine implements ICacheEngine {
8-
#cache: Map<string, { value: IData; expiresAt: number } | undefined>
5+
class MemoryCacheEngine implements CacheEngine {
6+
readonly #cache: Map<string, { value: CacheData; expiresAt: number } | undefined>
97

108
constructor() {
119
this.#cache = new Map()
1210
}
1311

14-
get(key: string): IData {
12+
get(key: string): CacheData {
1513
const item = this.#cache.get(key)
1614
if (!item || item.expiresAt < Date.now()) {
1715
this.del(key)
@@ -20,7 +18,7 @@ class MemoryCacheEngine implements ICacheEngine {
2018
return item.value
2119
}
2220

23-
set(key: string, value: IData, ttl?: number | StringValue): void {
21+
set(key: string, value: CacheData, ttl?: CacheTTL): void {
2422
const givenTTL = typeof ttl === 'string' ? ms(ttl) : ttl
2523
const actualTTL = givenTTL ?? Number.POSITIVE_INFINITY
2624
this.#cache.set(key, {

src/cache/engine/RedisCacheEngine.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import ms from 'ms'
55
import { convertToObject } from '../../version'
66

77
import type { Redis, RedisOptions } from 'ioredis'
8-
import type { StringValue } from 'ms'
9-
import type ICacheEngine from '../../interfaces/ICacheEngine'
10-
import type IData from '../../interfaces/IData'
8+
import type { CacheData, CacheEngine, CacheTTL } from '../../types'
119

12-
class RedisCacheEngine implements ICacheEngine {
13-
#client: Redis
10+
class RedisCacheEngine implements CacheEngine {
11+
readonly #client: Redis
1412

1513
constructor(options: RedisOptions) {
1614
if (!options.keyPrefix) {
@@ -19,20 +17,20 @@ class RedisCacheEngine implements ICacheEngine {
1917
this.#client = new IORedis(options)
2018
}
2119

22-
async get(key: string): Promise<IData> {
20+
async get(key: string): Promise<CacheData> {
2321
try {
2422
const value = await this.#client.get(key)
2523
if (value === null) {
2624
return undefined
2725
}
28-
return EJSON.parse(value) as IData
26+
return EJSON.parse(value) as CacheData
2927
} catch (err) {
3028
console.error(err)
3129
return undefined
3230
}
3331
}
3432

35-
async set(key: string, value: IData, ttl?: number | StringValue): Promise<void> {
33+
async set(key: string, value: CacheData, ttl?: CacheTTL): Promise<void> {
3634
try {
3735
const givenTTL = typeof ttl === 'string' ? ms(ttl) : ttl
3836
const actualTTL = givenTTL ?? Number.POSITIVE_INFINITY

src/extend/aggregate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { getKey } from '../key'
22

33
import type { Mongoose } from 'mongoose'
4-
import type { StringValue } from 'ms'
54
import type Cache from '../cache/Cache'
5+
import type { CacheTTL } from '../types'
66

77
export default function extendQuery(mongoose: Mongoose, cache: Cache): void {
88
const mongooseExec = mongoose.Aggregate.prototype.exec
@@ -19,7 +19,7 @@ export default function extendQuery(mongoose: Mongoose, cache: Cache): void {
1919
return this._ttl
2020
}
2121

22-
mongoose.Aggregate.prototype.cache = function (ttl?: number | StringValue, customKey?: string) {
22+
mongoose.Aggregate.prototype.cache = function (ttl?: CacheTTL, customKey?: string) {
2323
this._ttl = ttl ?? null
2424
this._key = customKey ?? null
2525
return this

src/extend/query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { getKey } from '../key'
22

33
import type { Mongoose } from 'mongoose'
4-
import type { StringValue } from 'ms'
54
import type Cache from '../cache/Cache'
5+
import type { CacheTTL } from '../types'
66

77
export default function extendQuery(mongoose: Mongoose, cache: Cache): void {
88
const mongooseExec = mongoose.Query.prototype.exec
@@ -33,7 +33,7 @@ export default function extendQuery(mongoose: Mongoose, cache: Cache): void {
3333
return this._ttl
3434
}
3535

36-
mongoose.Query.prototype.cache = function (ttl?: number | StringValue, customKey?: string) {
36+
mongoose.Query.prototype.cache = function (ttl?: CacheTTL, customKey?: string) {
3737
this._ttl = ttl ?? null
3838
this._key = customKey ?? null
3939
return this

src/interfaces/ICacheEngine.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/interfaces/ICacheOptions.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/interfaces/IData.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/plugin.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import extendAggregate from './extend/aggregate'
33
import extendQuery from './extend/query'
44

55
import type { Mongoose } from 'mongoose'
6-
import type { StringValue } from 'ms'
7-
import type ICacheOptions from './interfaces/ICacheOptions'
6+
import type { CacheOptions, CacheTTL } from './types'
87

98
declare module 'mongoose' {
109
interface Query<ResultType, DocType, THelpers, RawDocType> {
11-
cache: (this: Query<ResultType, DocType, THelpers, RawDocType>, ttl?: number | StringValue, customKey?: string) => this
10+
cache: (this: Query<ResultType, DocType, THelpers, RawDocType>, ttl?: CacheTTL, customKey?: string) => this
1211
_key: string | null
1312
getCacheKey: (this: Query<ResultType, DocType, THelpers, RawDocType>) => string
14-
_ttl: number | StringValue | null
15-
getCacheTTL: (this: Query<ResultType, DocType, THelpers, RawDocType>) => number | StringValue | null
13+
_ttl: CacheTTL | null
14+
getCacheTTL: (this: Query<ResultType, DocType, THelpers, RawDocType>) => CacheTTL | null
1615
op?: string
1716
_path?: unknown
1817
_fields?: unknown
@@ -21,11 +20,11 @@ declare module 'mongoose' {
2120
}
2221

2322
interface Aggregate<ResultType> {
24-
cache: (this: Aggregate<ResultType>, ttl?: number | StringValue, customKey?: string) => this
23+
cache: (this: Aggregate<ResultType>, ttl?: CacheTTL, customKey?: string) => this
2524
_key: string | null
2625
getCacheKey: (this: Aggregate<ResultType>) => string
27-
_ttl: number | StringValue | null
28-
getCacheTTL: (this: Aggregate<ResultType>) => number | StringValue | null
26+
_ttl: CacheTTL | null
27+
getCacheTTL: (this: Aggregate<ResultType>) => CacheTTL | null
2928
}
3029
}
3130

@@ -37,7 +36,7 @@ class CacheMongoose {
3736
// Private constructor to prevent external instantiation
3837
}
3938

40-
public static init(mongoose: Mongoose, cacheOptions: ICacheOptions): CacheMongoose {
39+
public static init(mongoose: Mongoose, cacheOptions: CacheOptions): CacheMongoose {
4140
if (!CacheMongoose.#instance) {
4241
CacheMongoose.#instance = new CacheMongoose()
4342
CacheMongoose.#instance.cache = new Cache(cacheOptions)

src/types.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { RedisOptions } from 'ioredis'
2+
import type { StringValue } from 'ms'
3+
4+
export type CacheTTL = number | StringValue
5+
6+
export type CacheData = Record<string, unknown> | Record<string, unknown>[] | unknown[] | number | undefined
7+
8+
export interface CacheEngine {
9+
get: (key: string) => Promise<CacheData> | CacheData
10+
set: (key: string, value: CacheData, ttl?: CacheTTL) => Promise<void> | void
11+
del: (key: string) => Promise<void> | void
12+
clear: () => Promise<void> | void
13+
close: () => Promise<void> | void
14+
}
15+
16+
export interface CacheOptions {
17+
engine: 'memory' | 'redis'
18+
engineOptions?: RedisOptions
19+
defaultTTL?: CacheTTL
20+
debug?: boolean
21+
}

src/version.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import mongoose from 'mongoose'
22
import { satisfies } from 'semver'
33

4-
import type IData from './interfaces/IData'
4+
import type { CacheData } from './types'
55

66
export const isMongooseLessThan7 = satisfies(mongoose.version, '<7')
77

8-
export const convertToObject = <T>(value: (T & { toObject?: () => IData }) | undefined): IData => {
8+
export const convertToObject = <T>(value: (T & { toObject?: () => CacheData }) | undefined): CacheData => {
99
if (isMongooseLessThan7) {
1010
if (value != null && typeof value === 'object' && !Array.isArray(value) && value.toObject) {
1111
return value.toObject()

tests/cache-options.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { describe, expect, it, vi } from 'vitest'
22

33
import Cache from '../src/cache/Cache'
44

5-
import type ICacheOptions from '../src/interfaces/ICacheOptions'
5+
import type { CacheOptions } from '../src/types'
66

77
describe('Cache class tests', () => {
88
it('should create a new instance of Cache', () => {
9-
const cacheOptions: ICacheOptions = {
9+
const cacheOptions: CacheOptions = {
1010
engine: 'memory',
1111
}
1212

@@ -20,7 +20,7 @@ describe('Cache class tests', () => {
2020
})
2121

2222
it('should throw an error if the cache engine is not supported', () => {
23-
const cacheOptions: ICacheOptions = {
23+
const cacheOptions: CacheOptions = {
2424
// @ts-expect-error Testing invalid engine name
2525
engine: 'not-supported',
2626
}
@@ -29,15 +29,15 @@ describe('Cache class tests', () => {
2929
})
3030

3131
it('should throw an error if the cache engine is redis and no engine options are provided', () => {
32-
const cacheOptions: ICacheOptions = {
32+
const cacheOptions: CacheOptions = {
3333
engine: 'redis',
3434
}
3535

3636
expect(() => new Cache(cacheOptions)).toThrow(`Engine options are required for ${cacheOptions.engine} engine`)
3737
})
3838

3939
it('should create a new instance of Cache with redis engine', async () => {
40-
const cacheOptions: ICacheOptions = {
40+
const cacheOptions: CacheOptions = {
4141
engine: 'redis',
4242
engineOptions: {
4343
host: 'localhost',

0 commit comments

Comments
 (0)