Skip to content

Commit

Permalink
增加清理缓存功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed Sep 1, 2024
1 parent d88a28e commit 3b9d484
Show file tree
Hide file tree
Showing 19 changed files with 201 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/small-islands-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@opendoc/frontend": minor
"@opendoc/backend": minor
---

增加清理缓存功能
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { SheetVersionService } from './sheet-version.service'
import { QuerySheetVersionsResponseDTO } from './dto/query-sheet-versions-response.dto'
import { QuerySheetVersionsDTO } from './dto/query-sheet-versions.dto'
import { SheetVersion } from './entities/sheet-version.entity'
import { ApiTags } from '@nestjs/swagger'
import { ApiInternalServerErrorResponse, ApiTags } from '@nestjs/swagger'


@ApiTags('Sheet Version', '版本')
@Controller('sheet-version')
@ApiInternalServerErrorResponse({ description: '系统异常' })
export class SheetVersionController {
constructor(
private readonly sheetVersionService: SheetVersionService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class SheetPullCrontabSubscriber implements EventSubscriber<SheetPullCron
private readonly orm: MikroORM,

private readonly eventEmitter: EventEmitter2,
private readonly sheetSynchronizeService: SheetSynchronizeService
private readonly sheetSynchronizeService: SheetSynchronizeService,
) {
em.getEventManager().registerSubscriber(this)
}
Expand Down
12 changes: 8 additions & 4 deletions app/backend/src/modules/storage/cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as path from 'path'
import * as fs from 'fs-extra'
import { Injectable } from "@nestjs/common";
import { StorageConfig } from "~/config/storage.config";
import { Readable } from 'stream';
import { Injectable } from '@nestjs/common'
import { StorageConfig } from '~/config/storage.config'
import { Readable } from 'stream'

@Injectable()
export class CacheService {
readonly directory: string;
readonly directory: string

constructor(
private readonly storageConfig: StorageConfig,
Expand Down Expand Up @@ -51,4 +51,8 @@ export class CacheService {
const stream = fs.createReadStream(cacheFilepath)
return intoStream(stream)
}

async clear(): Promise<void> {
await fs.emptyDir(this.directory)
}
}
19 changes: 19 additions & 0 deletions app/backend/src/modules/storage/storage.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Controller, Delete } from '@nestjs/common'
import { CacheService } from './cache.service'
import { ApiInternalServerErrorResponse, ApiOperation, ApiTags } from '@nestjs/swagger'


@ApiTags('Storage', '存储')
@Controller('storage')
@ApiInternalServerErrorResponse({ description: '系统异常' })
export class StorageController {
constructor(
private readonly cacheService: CacheService,
) {}

@ApiOperation({ summary: '清空缓存' })
@Delete('cache')
async clearStorageCache(): Promise<void> {
await this.cacheService.clear()
}
}
3 changes: 2 additions & 1 deletion app/backend/src/modules/storage/storage.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { DiskService } from './disk.service'
import { S3Service } from './s3.service'
import { OssService } from './oss.service'
import { CacheService } from './cache.service'
import { StorageController } from './storage.controller'


@Module({
imports: [],
controllers: [],
controllers: [StorageController],
providers: [
CacheService,
StorageService,
Expand Down
16 changes: 16 additions & 0 deletions app/frontend/api/backend/clear_storage_cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Keq } from 'keq'
import { request } from 'keq'
import type { RequestParameters, ResponseMap, Operation } from "./types/clear_storage_cache"



export function clearStorageCache<STATUS extends keyof ResponseMap>(arg?: RequestParameters): Keq<ResponseMap[STATUS], Operation<STATUS>> {
const req = request.delete<ResponseMap[STATUS]>("/api/storage/cache")
.option('module', {
name: "backend",
pathname: "/api/storage/cache",
})


return req as unknown as Keq<ResponseMap[STATUS], Operation<STATUS>>
}
1 change: 1 addition & 0 deletions app/frontend/api/backend/components/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from "./parsed_version_dto"
export * from "./plugin"
export * from "./plugin_command_message"
export * from "./plugin_event_message"
export * from "./plugin_logs"
export * from "./plugin_metadata"
export * from "./plugin_option"
export * from "./plugin_option_definition"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @export
*/
export interface PluginCommandMessage {
"command": "join" | "create-sdk" | "update-sdk"
"command": "join" | "create-sdk" | "update-sdk" | "log"
"data": {
}
}
7 changes: 7 additions & 0 deletions app/frontend/api/backend/components/schemas/plugin_logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @interface PluginLogs
* @export
*/
export interface PluginLogs {
"message": string
}
1 change: 1 addition & 0 deletions app/frontend/api/backend/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./check_health"
export * from "./clear_storage_cache"
export * from "./create_application"
export * from "./create_example"
export * from "./create_forbidden_application_code"
Expand Down
29 changes: 29 additions & 0 deletions app/frontend/api/backend/types/clear_storage_cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { KeqOperation } from 'keq'


export interface ResponseMap {
"200": unknown
"500": unknown
}


export type QueryParameters = {
}

export type RouteParameters = {
}

export type HeaderParameters = {
}

export type BodyParameters ={}
export type RequestParameters = QueryParameters & RouteParameters & HeaderParameters & BodyParameters


export interface Operation<STATUS extends keyof ResponseMap> extends KeqOperation {
requestParams: RouteParameters
requestQuery: QueryParameters
requestHeaders: HeaderParameters
requestBody: BodyParameters
responseBody: ResponseMap[STATUS]
}
1 change: 1 addition & 0 deletions app/frontend/api/backend/types/query_sheet_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { SheetVersion } from "../components/schemas/sheet_version"

export interface ResponseMap {
"200": SheetVersion
"500": unknown
}


Expand Down
1 change: 1 addition & 0 deletions app/frontend/api/backend/types/query_sheet_versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { QuerySheetVersionsResponseDTO } from "../components/schemas/query_

export interface ResponseMap {
"200": QuerySheetVersionsResponseDTO
"500": unknown
}


Expand Down
27 changes: 27 additions & 0 deletions app/frontend/components/basic/button.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script setup lang="ts">
defineProps<{
loading: boolean
}>()
defineEmits<{
click: [MouseEvent]
}>()
</script>

<template>
<button
class="d-btn"
:disabled="loading"
@click="$emit('click', $event)"
>
<span
v-if="loading"
class="d-loading d-loading-sm d-loading-spinner"
/>

<slot />
</button>
</template>

<style scoped lang="postcss">
</style>
27 changes: 27 additions & 0 deletions app/frontend/components/form/operation.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script setup lang="ts">
defineProps<{
pending: boolean
}>()
defineEmits<{
click: []
}>()
</script>

<template>
<div class="flex justify-between my-8 hover:bg-ctp-overlay0/10 rounded-sm py-2 px-3">
<div class="flex flex-col justify-center">
<span class="font-bold">
<slot name="title" />
</span>
<span class="text-sm text-base-content/70">
<slot name="description" />
</span>
</div>

<slot />
</div>
</template>

<style scoped lang="postcss">
</style>
14 changes: 9 additions & 5 deletions app/frontend/composables/use-async-fn.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
interface UseAsyncFnReturnType<T extends (...args: any[]) => Promise<any>> {
type PromiseFunction = (...args: any[]) => Promise<any>

interface UseAsyncFnReturnType<T extends PromiseFunction> {
pending: Ref<boolean>
error: Ref<Error | null>
execute: T
}

export function useAsyncFn<T extends (...args: any[]) => Promise<any>> (fn: T): UseAsyncFnReturnType<T> {
export function useAsyncFn<T extends PromiseFunction>(fn: T): UseAsyncFnReturnType<T> {
const pending = ref(false)
const error = ref<Error | null>(null)

async function execute (...args: Parameters<T>) {
async function execute(...args: Parameters<T>): Promise<void> {
pending.value = true
error.value = null

try {
return await fn(...args)
} catch (err) {
}
catch (err) {
error.value = err as Error
} finally {
}
finally {
pending.value = false
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/frontend/pages/administration.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const router = useRouter()
</div>

<ul class="flex-0 flex-nowrap d-menu w-72">
<li>
<NuxtLink
to="/administration/console"
active-class="d-active"
>
<span>控制台</span>
</NuxtLink>
</li>

<li>
<NuxtLink
to="/administration/forbidden-application-code-management"
Expand Down
34 changes: 34 additions & 0 deletions app/frontend/pages/administration/console.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script setup lang="ts">
import { clearStorageCache } from '~/api/backend'
const alert = useAlert()
const { pending: clearing, execute: clearCache } = useAsyncFn(async () => {
await clearStorageCache()
alert.success('缓存已清除')
})
</script>

<template>
<div>
<form-operation>
<template #title>
清除缓存
</template>

<template #description>
清除应用、页签、文档的缓存数据
</template>

<basic-button
:loading="clearing"
@click="clearCache"
>
清除
</basic-button>
</form-operation>
</div>
</template>

<style scoped lang="postcss">
</style>

0 comments on commit 3b9d484

Please sign in to comment.