import { Provide, Scope, ScopeEnum, Inject } from "@midwayjs/decorator";
import { RunInPrimary } from "@midwayjs/process-agent";
import { CacheManager } from "@midwayjs/cache";

// 单例
@Provide()
@Scope(ScopeEnum.Singleton)
export default class CacheService {
  @Inject()
  private cache: CacheManager;

  // 设置缓存
  @RunInPrimary()
  async set(key: string, val: any, ttl = 5) {
    await this.cache.set(key, val, { ttl });
  }

  // 获取缓存
  @RunInPrimary()
  async get(key: string) {
    return await this.cache.get(key);
  }
}
