|
@@ -9,7 +9,12 @@ import {
|
|
Query,
|
|
Query,
|
|
UsePipes,
|
|
UsePipes,
|
|
ValidationPipe,
|
|
ValidationPipe,
|
|
|
|
+ CACHE_MANAGER,
|
|
|
|
+ Inject,
|
|
|
|
+ UseInterceptors,
|
|
|
|
+ CacheInterceptor,
|
|
} from '@nestjs/common';
|
|
} from '@nestjs/common';
|
|
|
|
+import { Cache } from 'cache-manager';
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
import { CollectionsService } from './collections.service';
|
|
import { CollectionsService } from './collections.service';
|
|
import {
|
|
import {
|
|
@@ -18,20 +23,38 @@ import {
|
|
ShowCollections,
|
|
ShowCollections,
|
|
VectorSearch,
|
|
VectorSearch,
|
|
} from './dto';
|
|
} from './dto';
|
|
|
|
+import { cacheKeys } from '../cache/config';
|
|
|
|
|
|
|
|
+//Including 2 kind of cache check getCollections and getStatistics for detail
|
|
@ApiTags('collections')
|
|
@ApiTags('collections')
|
|
@Controller('collections')
|
|
@Controller('collections')
|
|
export class CollectionsController {
|
|
export class CollectionsController {
|
|
- constructor(private collectionsService: CollectionsService) {}
|
|
|
|
|
|
+ constructor(private collectionsService: CollectionsService, @Inject(CACHE_MANAGER) private cacheManager: Cache) { }
|
|
|
|
|
|
|
|
+ // manually control cache if logic is complicated
|
|
@Get()
|
|
@Get()
|
|
async getCollections(@Query() data?: ShowCollections) {
|
|
async getCollections(@Query() data?: ShowCollections) {
|
|
- return Number(data.type) === 1
|
|
|
|
- ? await this.collectionsService.getLoadedColletions()
|
|
|
|
- : await this.collectionsService.getAllCollections();
|
|
|
|
|
|
+ if (Number(data.type) === 1) {
|
|
|
|
+ let loadedCollections = await this.cacheManager.get(cacheKeys.LOADEDCOLLECTIONS);
|
|
|
|
+ if (loadedCollections) {
|
|
|
|
+ return loadedCollections;
|
|
|
|
+ }
|
|
|
|
+ loadedCollections = await this.collectionsService.getLoadedColletions();
|
|
|
|
+ await this.cacheManager.set(cacheKeys.LOADEDCOLLECTIONS, loadedCollections);
|
|
|
|
+ return loadedCollections;
|
|
|
|
+ }
|
|
|
|
+ let allCollections = await this.cacheManager.get(cacheKeys.ALLCOLLECTIONS);
|
|
|
|
+ if (allCollections) {
|
|
|
|
+ return allCollections;
|
|
|
|
+ }
|
|
|
|
+ allCollections = await this.collectionsService.getAllCollections();
|
|
|
|
+ await this.cacheManager.set(cacheKeys.ALLCOLLECTIONS, allCollections);
|
|
|
|
+ return allCollections;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // use interceptor to control cache automatically
|
|
@Get('statistics')
|
|
@Get('statistics')
|
|
|
|
+ @UseInterceptors(CacheInterceptor)
|
|
async getStatistics() {
|
|
async getStatistics() {
|
|
return await this.collectionsService.getStatistics();
|
|
return await this.collectionsService.getStatistics();
|
|
}
|
|
}
|
|
@@ -39,12 +62,14 @@ export class CollectionsController {
|
|
@Post()
|
|
@Post()
|
|
@UsePipes(new ValidationPipe())
|
|
@UsePipes(new ValidationPipe())
|
|
async createCollection(@Body() data: CreateCollection) {
|
|
async createCollection(@Body() data: CreateCollection) {
|
|
|
|
+ await this.cacheManager.del(cacheKeys.ALLCOLLECTIONS);
|
|
return await this.collectionsService.createCollection(data);
|
|
return await this.collectionsService.createCollection(data);
|
|
}
|
|
}
|
|
|
|
|
|
@Delete(':name')
|
|
@Delete(':name')
|
|
// todo: need check some special symbols
|
|
// todo: need check some special symbols
|
|
async deleteCollection(@Param('name') name: string) {
|
|
async deleteCollection(@Param('name') name: string) {
|
|
|
|
+ await this.cacheManager.del(cacheKeys.ALLCOLLECTIONS);
|
|
return await this.collectionsService.dropCollection({
|
|
return await this.collectionsService.dropCollection({
|
|
collection_name: name,
|
|
collection_name: name,
|
|
});
|
|
});
|
|
@@ -71,6 +96,7 @@ export class CollectionsController {
|
|
|
|
|
|
@Put(':name/load')
|
|
@Put(':name/load')
|
|
async loadCollection(@Param('name') name: string) {
|
|
async loadCollection(@Param('name') name: string) {
|
|
|
|
+ await this.cacheManager.del(cacheKeys.LOADEDCOLLECTIONS);
|
|
return await this.collectionsService.loadCollection({
|
|
return await this.collectionsService.loadCollection({
|
|
collection_name: name,
|
|
collection_name: name,
|
|
});
|
|
});
|
|
@@ -78,6 +104,7 @@ export class CollectionsController {
|
|
|
|
|
|
@Put(':name/release')
|
|
@Put(':name/release')
|
|
async releaseCollection(@Param('name') name: string) {
|
|
async releaseCollection(@Param('name') name: string) {
|
|
|
|
+ await this.cacheManager.del(cacheKeys.LOADEDCOLLECTIONS);
|
|
return await this.collectionsService.releaseCollection({
|
|
return await this.collectionsService.releaseCollection({
|
|
collection_name: name,
|
|
collection_name: name,
|
|
});
|
|
});
|
|
@@ -85,6 +112,7 @@ export class CollectionsController {
|
|
|
|
|
|
@Post(':name/insert')
|
|
@Post(':name/insert')
|
|
async insertData(@Param('name') name: string, @Body() data: InsertData) {
|
|
async insertData(@Param('name') name: string, @Body() data: InsertData) {
|
|
|
|
+ await this.cacheManager.del(cacheKeys.ALLCOLLECTIONS);
|
|
return await this.collectionsService.insert({
|
|
return await this.collectionsService.insert({
|
|
collection_name: name,
|
|
collection_name: name,
|
|
...data,
|
|
...data,
|