-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement api-cache-data-access module for api caching
- Loading branch information
Showing
19 changed files
with
324 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@nrwl/web/babel", | ||
{ | ||
"useBuiltIns": "usage" | ||
} | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": ["../../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# api-cache-data-access | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test api-cache-data-access` to execute the unit tests via [Jest](https://jestjs.io). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'api-cache-data-access', | ||
preset: '../../../../jest.preset.js', | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
}, | ||
}, | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../../coverage/libs/api/cache/data-access', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "api-cache-data-access", | ||
"$schema": "../../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/api/cache/data-access/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nrwl/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["libs/api/cache/data-access/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nrwl/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/api/cache/data-access/jest.config.ts", | ||
"passWithNoTests": true | ||
} | ||
} | ||
}, | ||
"tags": ["scope:api", "type:data-access"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './lib/api-cache-data-access.service' | ||
export * from './lib/api-cache-data-access.module' |
26 changes: 26 additions & 0 deletions
26
libs/api/cache/data-access/src/lib/api-cache-data-access.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ApiConfigDataAccessModule, ApiConfigDataAccessService } from '@kin-kinetic/api/config/data-access' | ||
import { CacheModule, Module } from '@nestjs/common' | ||
import * as redisStore from 'cache-manager-redis-store' | ||
|
||
import { ApiCacheDataAccessService } from './api-cache-data-access.service' | ||
|
||
@Module({ | ||
imports: [ | ||
CacheModule.registerAsync({ | ||
imports: [ApiConfigDataAccessModule], | ||
inject: [ApiConfigDataAccessService], | ||
isGlobal: false, | ||
useFactory: (cfg) => ({ | ||
store: redisStore, | ||
host: cfg.redisHost, | ||
port: cfg.redisPort, | ||
username: cfg.redisUsername, | ||
password: cfg.redisPassword, | ||
ttl: 5, | ||
}), | ||
}), | ||
], | ||
providers: [ApiCacheDataAccessService], | ||
exports: [ApiCacheDataAccessService], | ||
}) | ||
export class ApiCacheDataAccessModule {} |
18 changes: 18 additions & 0 deletions
18
libs/api/cache/data-access/src/lib/api-cache-data-access.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test } from '@nestjs/testing' | ||
import { ApiCacheDataAccessService } from './api-cache-data-access.service' | ||
|
||
describe('ApiCacheDataAccessService', () => { | ||
let service: ApiCacheDataAccessService | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
providers: [ApiCacheDataAccessService], | ||
}).compile() | ||
|
||
service = module.get(ApiCacheDataAccessService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeTruthy() | ||
}) | ||
}) |
50 changes: 50 additions & 0 deletions
50
libs/api/cache/data-access/src/lib/api-cache-data-access.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { CACHE_MANAGER, Inject, Injectable, Logger } from '@nestjs/common' | ||
import { style } from '@ogma/styler' | ||
import { Cache } from 'cache-manager' | ||
|
||
export type CacheNamespace = 'solana' | ||
|
||
export function getCacheKey(namespace: CacheNamespace, key: string) { | ||
return `kinetic:${namespace}:${key}` | ||
} | ||
|
||
@Injectable() | ||
export class ApiCacheDataAccessService { | ||
private readonly logger = new Logger(ApiCacheDataAccessService.name) | ||
constructor(@Inject(CACHE_MANAGER) private readonly cache: Cache) {} | ||
|
||
del(namespace: CacheNamespace, key: string) { | ||
return this.cache.del(getCacheKey(namespace, key)) | ||
} | ||
|
||
/** | ||
* Get a value from the cache, or set it if it doesn't exist. | ||
*/ | ||
async wrap<T>(namespace: CacheNamespace, key: string, fn: () => Promise<T>, ttl?: number): Promise<T> { | ||
const cacheKey = getCacheKey(namespace, key) | ||
const found = await this.get<T>(namespace, key) | ||
if (!found) { | ||
const result = await fn() | ||
await this.set<T>(namespace, key, result, ttl) | ||
this.logger.verbose(`${style.bYellow.apply('[CACHE MISS]')} ${cacheKey} ttl=${ttl} seconds`) | ||
return result | ||
} | ||
|
||
this.logger.verbose(`${style.bGreen.apply('[CACHE HIT]')} ${cacheKey} ttl=${ttl} seconds`) | ||
return found | ||
} | ||
|
||
/** | ||
* Get a value from the cache based on the namespace and key. | ||
*/ | ||
private get<T>(namespace: CacheNamespace, key: string) { | ||
return this.cache.get<T>(getCacheKey(namespace, key)) | ||
} | ||
|
||
/** | ||
* Set a value in the cache based on the namespace and key. | ||
*/ | ||
private set<T>(namespace: CacheNamespace, key: string, value: T, ttl?: number) { | ||
return this.cache.set<T>(getCacheKey(namespace, key), value, { ttl: ttl ?? 5 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"extends": "../../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "commonjs" | ||
}, | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
}, | ||
{ | ||
"path": "./tsconfig.spec.json" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../../dist/out-tsc", | ||
"declaration": true, | ||
"types": ["node"], | ||
"target": "es6" | ||
}, | ||
"include": ["**/*.ts"], | ||
"exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"] | ||
}, | ||
"include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"] | ||
} |
3 changes: 2 additions & 1 deletion
3
libs/api/core/data-access/src/lib/api-core-data-access.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
libs/api/solana/data-access/src/lib/api-solana-data-access.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.