Skip to content

Commit

Permalink
feat(cache): change this of decorator methods from WebContext to the …
Browse files Browse the repository at this point in the history
…class decorated

remove validation of WebContext
  • Loading branch information
waitingsong committed Jul 28, 2024
1 parent 3e3e340 commit f8d69a8
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 67 deletions.
6 changes: 5 additions & 1 deletion packages/cache/src/lib/cacheable/cacheable.handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Singleton } from '@midwayjs/core'
import { Singleton, Inject } from '@midwayjs/core'
import { TraceService } from '@mwcp/otel'
import { DecoratorExecutorParamBase } from '@mwcp/share'

import { DecoratorHandlerCacheBase } from '../decorator.handler.types.js'
Expand All @@ -13,11 +14,14 @@ import { before, around } from './cacheable.helper.js'
*/
@Singleton()
export class DecoratorHandlerCacheable extends DecoratorHandlerCacheBase {
@Inject() readonly traceService: TraceService

override genExecutorParam(options: DecoratorExecutorParamBase): DecoratorExecutorOptions {
const optsExt: GenDecoratorExecutorOptionsExt = {
config: this.cacheConfig,
cachingFactory: this.cachingFactory,
op: 'cacheable',
traceService: this.traceService,
}
const ret = genDecoratorExecutorOptions(options, optsExt)
return ret
Expand Down
7 changes: 2 additions & 5 deletions packages/cache/src/lib/cacheable/cacheable.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import type { CachedResponse, CacheableArgs, DecoratorExecutorOptions } from '..

export async function before(options: DecoratorExecutorOptions<CacheableArgs<undefined>>): Promise<void> {
const {
webContext,
mergedDecoratorParam,
cachingFactory,
cachingInstanceId,
instance,
} = options
assert(webContext, 'webContext is undefined')

const mergedDecoratorParam2: CacheableArgs = {
...initCacheableArgs,
Expand All @@ -34,7 +33,7 @@ export async function before(options: DecoratorExecutorOptions<CacheableArgs<und
cacheName: mergedDecoratorParam2.cacheName,
methodArgs: options.methodArgs,
methodResult: options.methodResult,
webContext,
instance,
}
const cacheKey = genCacheKey(opts2)
options.mergedDecoratorParam = mergedDecoratorParam2
Expand All @@ -58,10 +57,8 @@ export async function before(options: DecoratorExecutorOptions<CacheableArgs<und

export async function around(options: DecoratorExecutorOptions<CacheableArgs<undefined>>): Promise<unknown> {
const {
webContext,
cacheKey,
} = options
assert(webContext, 'webContext is undefined')

if (options.methodResult) {
return options.methodResult
Expand Down
6 changes: 5 additions & 1 deletion packages/cache/src/lib/cacheevict/cacheevict.handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Singleton } from '@midwayjs/core'
import { Singleton, Inject } from '@midwayjs/core'
import { TraceService } from '@mwcp/otel'
import type { DecoratorExecutorParamBase } from '@mwcp/share'

import { DecoratorHandlerCacheBase } from '../decorator.handler.types.js'
Expand All @@ -14,11 +15,14 @@ import { around, before } from './cacheevict.helper.js'
*/
@Singleton()
export class DecoratorHandlerCacheEvict extends DecoratorHandlerCacheBase {
@Inject() readonly traceService: TraceService

override genExecutorParam(options: DecoratorExecutorParamBase): DecoratorExecutorOptions {
const optsExt: GenDecoratorExecutorOptionsExt = {
config: this.cacheConfig,
cachingFactory: this.cachingFactory,
op: 'cacheevict',
traceService: this.traceService,
}
const ret = genDecoratorExecutorOptions(options, optsExt)
return ret
Expand Down
10 changes: 4 additions & 6 deletions packages/cache/src/lib/cacheevict/cacheevict.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import type { CacheEvictArgs, DecoratorExecutorOptions } from '../types.js'

export async function before(options: DecoratorExecutorOptions<CacheEvictArgs>): Promise<void> {
const {
webContext,
instance,
mergedDecoratorParam,
} = options
assert(webContext, 'webContext is undefined')

const mergedDecoratorParam2: CacheEvictArgs = {
...initCacheEvictArgs,
Expand All @@ -21,7 +20,7 @@ export async function before(options: DecoratorExecutorOptions<CacheEvictArgs>):
...mergedDecoratorParam2,
methodArgs: options.methodArgs,
methodResult: void 0,
webContext,
instance,
}
const cacheKey = genCacheKey(opts2)
options.mergedDecoratorParam = mergedDecoratorParam2
Expand All @@ -30,13 +29,12 @@ export async function before(options: DecoratorExecutorOptions<CacheEvictArgs>):

export async function around(options: DecoratorExecutorOptions<CacheEvictArgs>): Promise<unknown> {
const {
webContext,
instance,
cachingFactory,
cachingInstanceId,
cacheKey,
mergedDecoratorParam,
} = options
assert(webContext, 'webContext is undefined')
assert(mergedDecoratorParam, 'mergedDecoratorParam is undefined')

const tmp = computerWriteConditionValue(options)
Expand Down Expand Up @@ -72,7 +70,7 @@ export async function around(options: DecoratorExecutorOptions<CacheEvictArgs>):
...options.mergedDecoratorParam as CacheEvictArgs,
methodArgs: options.methodArgs,
methodResult: options.methodResult,
webContext,
instance,
}
const cacheKey2 = genCacheKey(opts2)
cacheKey2 && await deleteData(caching, cacheKey2)
Expand Down
6 changes: 5 additions & 1 deletion packages/cache/src/lib/cacheput/cacheput.handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Singleton } from '@midwayjs/core'
import { Singleton, Inject } from '@midwayjs/core'
import { TraceService } from '@mwcp/otel'
import { DecoratorExecutorParamBase } from '@mwcp/share'

import { DecoratorHandlerCacheBase } from '../decorator.handler.types.js'
Expand All @@ -14,11 +15,14 @@ import { before, decoratorExecutor } from './cacheput.helper.js'
*/
@Singleton()
export class DecoratorHandlerCachePut extends DecoratorHandlerCacheBase {
@Inject() readonly traceService: TraceService

override genExecutorParam(options: DecoratorExecutorParamBase): DecoratorExecutorOptions {
const optsExt: GenDecoratorExecutorOptionsExt = {
config: this.cacheConfig,
cachingFactory: this.cachingFactory,
op: 'cacheput',
traceService: this.traceService,
}
const ret = genDecoratorExecutorOptions(options, optsExt)
return ret
Expand Down
10 changes: 4 additions & 6 deletions packages/cache/src/lib/cacheput/cacheput.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import type { CacheableArgs, CachedResponse, DecoratorExecutorOptions } from '..


export async function before(options: DecoratorExecutorOptions<CacheableArgs<undefined>>): Promise<void> {
const { webContext, mergedDecoratorParam } = options
assert(webContext, 'webContext is undefined')
const { instance, mergedDecoratorParam } = options

const mergedDecoratorParam2: CacheableArgs = {
...initCachePutArgs,
Expand All @@ -28,7 +27,7 @@ export async function before(options: DecoratorExecutorOptions<CacheableArgs<und
cacheName: mergedDecoratorParam2.cacheName,
methodArgs: options.methodArgs,
methodResult: options.methodResult,
webContext,
instance,
}
const cacheKey = genCacheKey(opts2)
options.mergedDecoratorParam = mergedDecoratorParam2
Expand All @@ -37,12 +36,11 @@ export async function before(options: DecoratorExecutorOptions<CacheableArgs<und

export async function decoratorExecutor(options: DecoratorExecutorOptions<CacheableArgs>): Promise<unknown> {
const {
webContext,
instance,
cachingFactory,
cachingInstanceId,
cacheKey,
} = options
assert(webContext, 'webContext is undefined')

const { method, methodArgs } = options
assert(method, 'original method invalid')
Expand Down Expand Up @@ -75,7 +73,7 @@ export async function decoratorExecutor(options: DecoratorExecutorOptions<Cachea
cacheName: mergedDecoratorParam.cacheName,
methodArgs: options.methodArgs,
methodResult: options.methodResult,
webContext,
instance,
}
const resp2 = genDataWithCacheMeta(cacheResp, opts2, ttl)
return resp2
Expand Down
55 changes: 20 additions & 35 deletions packages/cache/src/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@ import assert from 'node:assert'
import { createHash } from 'node:crypto'

import type { MidwayUnionCache } from '@midwayjs/cache-manager'
import { REQUEST_OBJ_CTX_KEY } from '@midwayjs/core'
import { OtelConfigKey } from '@mwcp/otel'
import type { AbstractTraceService } from '@mwcp/otel'
import type { DecoratorExecutorParamBase, PagingDTO, Context as WebContext } from '@mwcp/share'
import type { ClzInstance, DecoratorExecutorParamBase, PagingDTO } from '@mwcp/share'

import { initCacheableArgs, initCacheEvictArgs, initCacheManagerOptions } from './config.js'
import {




ConfigKey,



Msg,
} from './types.js'
import type { CacheableArgs, CachedResponse, CacheEvictArgs, CacheTTLFn, DataWithCacheMeta, DecoratorExecutorOptions, GenDecoratorExecutorOptionsExt } from './types.js'
import type {
CacheableArgs,
CachedResponse,
CacheEvictArgs,
CacheTTLFn,
DataWithCacheMeta,
DecoratorExecutorOptions,
GenDecoratorExecutorOptionsExt,
} from './types.js'


export interface GenCacheKeyOptions {
key: CacheableArgs['key']
cacheName: CacheableArgs['cacheName']
webContext: WebContext
instance: ClzInstance
methodArgs: unknown[]
methodResult?: unknown
}

export function genCacheKey(options: GenCacheKeyOptions): string | false {
const { key, cacheName, webContext, methodArgs, methodResult } = options
const { key, cacheName, instance, methodArgs, methodResult } = options
assert(cacheName, 'cacheName is undefined')

switch (typeof key) {
Expand All @@ -51,7 +50,7 @@ export function genCacheKey(options: GenCacheKeyOptions): string | false {

case 'function': {
// @ts-expect-error
const keyStr = key.call(webContext, methodArgs, methodResult)
const keyStr = key.call(instance, methodArgs, methodResult)
switch (typeof keyStr) {
case 'undefined': {
const key2 = serializeArgs(methodArgs)
Expand Down Expand Up @@ -244,12 +243,9 @@ export async function getData<T = unknown>(

export function computerWriteConditionValue(options: DecoratorExecutorOptions<CacheableArgs | CacheEvictArgs>): boolean | Promise<boolean> {

const { mergedDecoratorParam: cacheOptions } = options
const { mergedDecoratorParam: cacheOptions, instance } = options
assert(cacheOptions, 'cacheOptions is undefined within computerConditionValue()')

const webContext = options.instance[REQUEST_OBJ_CTX_KEY]
assert(webContext, 'webContext is undefined')

switch (typeof cacheOptions.writeCondition) {
case 'undefined':
return true
Expand All @@ -260,7 +256,7 @@ export function computerWriteConditionValue(options: DecoratorExecutorOptions<Ca
case 'function': {
const fn = cacheOptions.writeCondition as (...args: unknown[]) => boolean | Promise<boolean>
return fn.call(
webContext,
instance,
options.methodArgs,
options.methodResult,
)
Expand All @@ -272,12 +268,9 @@ export function computerWriteConditionValue(options: DecoratorExecutorOptions<Ca
}
export function computerReadConditionValue(options: DecoratorExecutorOptions<CacheableArgs>): boolean | Promise<boolean> {

const { mergedDecoratorParam: cacheOptions } = options
const { mergedDecoratorParam: cacheOptions, instance } = options
assert(cacheOptions, 'cacheOptions is undefined within computerReadConditionValue()')

const webContext = options.instance[REQUEST_OBJ_CTX_KEY]
assert(webContext, 'webContext is undefined')

switch (typeof cacheOptions.condition) {
case 'undefined':
return true
Expand All @@ -288,7 +281,7 @@ export function computerReadConditionValue(options: DecoratorExecutorOptions<Cac
case 'function': {
const fn = cacheOptions.condition as (...args: unknown[]) => boolean | Promise<boolean>
return fn.call(
webContext,
instance,
options.methodArgs,
options.methodResult,
)
Expand All @@ -304,12 +297,9 @@ export function computerTTLValue(
options: DecoratorExecutorOptions<CacheableArgs>,
): number | Promise<number> {

const { mergedDecoratorParam: cacheOptions } = options
const { mergedDecoratorParam: cacheOptions, instance } = options
assert(cacheOptions, 'cacheOptions is undefined within computerTTLValue()')

const webContext = options.instance[REQUEST_OBJ_CTX_KEY]
assert(webContext, 'webContext is undefined')

let ttl = 10 // second

switch (typeof cacheOptions.ttl) {
Expand All @@ -325,7 +315,7 @@ export function computerTTLValue(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fn = cacheOptions.ttl as CacheTTLFn<any>
return fn.call(
webContext,
instance,
options.methodArgs,
result,
)
Expand Down Expand Up @@ -380,16 +370,11 @@ export function genDecoratorExecutorOptions<T extends object>(
cacheOptions.cacheName = cacheName
}

let traceService
if (optionsBase.webContext) {
traceService = optionsBase.webContext[`_${OtelConfigKey.serviceName}`] as AbstractTraceService | undefined
assert(traceService, 'TraceService is not initialized. (TraceService 尚未初始化)')
}
assert(optionsExt.traceService, 'genDecoratorExecutorOptions(): traceService is undefined')

const ret: DecoratorExecutorOptions = {
...optionsBase,
...optionsExt,
traceService,
mergedDecoratorParam: cacheOptions,
}
return ret
Expand Down
Loading

0 comments on commit f8d69a8

Please sign in to comment.