ze-common
v1.0.0
Published
Common modules for ZE services
Downloads
4
Readme
ZE Common
ZE Common là một module chung chứa các thành phần được sử dụng bởi nhiều service trong hệ thống ZE.
Cài đặt
# Cài đặt dependencies
npm install
# Build module
npm run buildCấu trúc
ze-common/
├── src/
│ ├── cache/ # Module cache sử dụng Redis
│ │ ├── cache.module.ts
│ │ └── cache.service.ts
│ └── index.ts # Export các module
├── package.json
└── tsconfig.jsonSử dụng
Cache Module
Cache module cung cấp các chức năng caching sử dụng Redis:
import { CacheModule, CacheService } from 'ze-common';
@Module({
imports: [CacheModule],
providers: [YourService],
})
export class YourModule {}
@Injectable()
export class YourService {
constructor(private cacheService: CacheService) {}
async getData(key: string) {
// Lấy dữ liệu từ cache
const cachedData = await this.cacheService.get(key);
if (cachedData) {
return cachedData;
}
// Nếu không có trong cache, lấy từ nguồn khác
const data = await this.fetchData();
// Lưu vào cache
await this.cacheService.set(key, data, 1800); // TTL: 30 phút
return data;
}
}Cấu hình
Cache module sử dụng các biến môi trường sau:
REDIS_HOST: Host của Redis server (mặc định: localhost)REDIS_PORT: Port của Redis server (mặc định: 6379)
Phát triển
Thêm module mới
- Tạo thư mục mới trong
src/ - Tạo các file cần thiết (module, service, etc.)
- Export module trong
src/index.ts
Build
npm run buildTest
npm test