Skip to content

Commit

Permalink
feat: add midway cache (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-jin authored Mar 20, 2021
1 parent 2818920 commit cc49eee
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/cache/.gitignore
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.*
49 changes: 49 additions & 0 deletions packages/cache/package.json
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"
}
}
7 changes: 7 additions & 0 deletions packages/cache/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const cache = {
store: 'memory',
options: {
max: 100,
ttl: 10,
},
};
9 changes: 9 additions & 0 deletions packages/cache/src/configuration.ts
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.
2 changes: 2 additions & 0 deletions packages/cache/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { AutoConfiguration as Configuration } from './configuration';
export * from './service/cache';
47 changes: 47 additions & 0 deletions packages/cache/src/service/cache.ts
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();
}
}
23 changes: 23 additions & 0 deletions packages/cache/test/cache.test.ts
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)
})
})
3 changes: 3 additions & 0 deletions packages/cache/test/fixtures/cache-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "my-midway-project"
}
10 changes: 10 additions & 0 deletions packages/cache/test/fixtures/cache-manager/src/configuration.ts
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 {
}
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();
}
}
23 changes: 23 additions & 0 deletions packages/cache/tsconfig.json
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"
]
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export * from './util/pathFileUtil';
export * from './features';
export * from './util/webRouterParam';
export * from './util/webRouterCollector';
export * from './util/emptyFramework';
export { plainToClass, classToPlain } from 'class-transformer';
export * from './logger';
export { createConfiguration } from './functional/configuration';
Expand Down

0 comments on commit cc49eee

Please sign in to comment.