-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
211 additions
and
0 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,15 @@ | ||
logs/ | ||
npm-debug.log | ||
yarn-error.log | ||
node_modules/ | ||
package-lock.json | ||
yarn.lock | ||
coverage/ | ||
dist/ | ||
.idea/ | ||
run/ | ||
.DS_Store | ||
*.sw* | ||
*.un~ | ||
.tsbuildinfo | ||
.tsbuildinfo.* |
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,49 @@ | ||
{ | ||
"name": "@midwayjs/cache", | ||
"version": "2.8.11", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "midway-bin build -c", | ||
"test": "midway-bin test --ts", | ||
"cov": "midway-bin cov --ts", | ||
"ci": "npm run cov", | ||
"lint": "mwts check" | ||
}, | ||
"author": "", | ||
"files": [ | ||
"dist/**/*.js", | ||
"dist/**/*.d.ts" | ||
], | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:midwayjs/midway.git" | ||
}, | ||
"keywords": [ | ||
"midway", | ||
"cache" | ||
], | ||
"engines": { | ||
"node": ">= 10" | ||
}, | ||
"devDependencies": { | ||
"@midwayjs/cli": "^1.2.38", | ||
"@midwayjs/core": "^2.8.11", | ||
"@midwayjs/decorator": "^2.3.0", | ||
"@types/jest": "^26.0.10", | ||
"@types/node": "14", | ||
"cross-env": "^6.0.0", | ||
"jest": "^26.4.0", | ||
"mwts": "^1.0.5", | ||
"ts-jest": "^26.2.0", | ||
"typescript": "^4.0.0", | ||
"@types/cache-manager": "^3.4.0", | ||
"cache-manager": "^3.4.1" | ||
}, | ||
"peerDependencies": { | ||
"@types/cache-manager": "^3.4.0", | ||
"cache-manager": "^3.4.1" | ||
} | ||
} |
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 @@ | ||
export const cache = { | ||
store: 'memory', | ||
options: { | ||
max: 100, | ||
ttl: 10, | ||
}, | ||
}; |
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 @@ | ||
// src/configuration.ts | ||
import { Configuration } from '@midwayjs/decorator'; | ||
import { join } from 'path'; | ||
|
||
@Configuration({ | ||
namespace: 'cache', | ||
importConfigs: [join(__dirname, 'config')], | ||
}) | ||
export class AutoConfiguration {} |
Empty file.
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 { AutoConfiguration as Configuration } from './configuration'; | ||
export * from './service/cache'; |
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,47 @@ | ||
import { Config, Init, Provide, Scope, ScopeEnum } from '@midwayjs/decorator'; | ||
import * as cacheManager from 'cache-manager'; | ||
|
||
@Scope(ScopeEnum.Singleton) | ||
@Provide() | ||
export class CacheManager { | ||
cache: cacheManager.Cache; | ||
|
||
@Config('cache') | ||
cacheConfig; | ||
|
||
@Init() | ||
async init() { | ||
this.cache = cacheManager.caching({ | ||
store: this.cacheConfig.store, | ||
...this.cacheConfig.options, | ||
}); | ||
} | ||
|
||
// 获取key | ||
async get(key: string) { | ||
return new Promise((resolve, reject) => { | ||
this.cache.get(key, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(result); | ||
}); | ||
}); | ||
} | ||
|
||
// 设置cache | ||
async set(key: string, value: string, options?: cacheManager.CachingConfig) { | ||
return await this.cache.set(key, value, options); | ||
} | ||
|
||
// 删除key | ||
async del(key: string) { | ||
return await this.cache.del(key); | ||
} | ||
|
||
// 清空cache | ||
async reset() { | ||
return await this.cache.reset(); | ||
} | ||
} |
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,23 @@ | ||
|
||
import { LightFramework } from '@midwayjs/core'; | ||
import * as path from 'path'; | ||
import * as assert from 'assert'; | ||
|
||
describe(`test.cache`, ()=>{ | ||
it(`test cache`, async ()=>{ | ||
const framework = new LightFramework(); | ||
await framework.initialize({ | ||
baseDir: path.join(__dirname, './fixtures/cache-manager/src'), | ||
isTsMode: true, | ||
}); | ||
|
||
const appCtx = framework.getApplicationContext(); | ||
|
||
const userService: any = await appCtx.getAsync('userService'); | ||
assert((await userService.getUser(`name`)) === undefined) | ||
await userService.setUser('name', 'stone-jin') | ||
assert((await userService.getUser(`name`)) === 'stone-jin') | ||
await userService.reset(); | ||
assert((await userService.getUser(`name`)) === undefined) | ||
}) | ||
}) |
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,3 @@ | ||
{ | ||
"name": "my-midway-project" | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/cache/test/fixtures/cache-manager/src/configuration.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,10 @@ | ||
import { Configuration } from '@midwayjs/decorator'; | ||
import * as cacheComponent from '../../../../src/index'; | ||
|
||
@Configuration({ | ||
imports: [ | ||
cacheComponent | ||
] | ||
}) | ||
export class ContainerLifeCycle { | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/cache/test/fixtures/cache-manager/src/service/user.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,22 @@ | ||
import { Inject, Provide } from '@midwayjs/decorator'; | ||
import { CacheManager } from '../../../../../src/index'; | ||
|
||
@Provide() | ||
export class UserService { | ||
|
||
@Inject(`cache:cacheManager`) | ||
cache: CacheManager; | ||
|
||
async setUser(name: string, value: string){ | ||
await this.cache.set(name, value); | ||
} | ||
|
||
async getUser(name: string){ | ||
let result = await this.cache.get(name); | ||
return result; | ||
} | ||
|
||
async reset(){ | ||
await this.cache.reset(); | ||
} | ||
} |
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,23 @@ | ||
{ | ||
"compileOnSave": true, | ||
"compilerOptions": { | ||
"target": "ES2018", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"inlineSourceMap":false, | ||
"noImplicitThis": true, | ||
"noUnusedLocals": true, | ||
"stripInternal": true, | ||
"noImplicitReturns": false, | ||
"pretty": true, | ||
"declaration": true, | ||
"outDir": "dist" | ||
}, | ||
"exclude": [ | ||
"dist", | ||
"node_modules", | ||
"test" | ||
] | ||
} |
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