"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheModule = void 0;
const cache_1 = require("@midwayjs/cache");
const decorator_1 = require("@midwayjs/decorator");
let CacheModule = class CacheModule {
    /**
     * 获取缓存key
     * @param pattern 匹配项
     */
    async keys(pattern) {
        var _a, _b;
        const keyPrefix = ((_b = (_a = this.cacheManager.cacheConfig) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.keyPrefix) || '';
        if (this.cacheManager.cache.store['name'] === 'memory') {
            return ((await this.cacheManager.cache.store.keys()) || []).map(key => key.replace(keyPrefix, ''));
        }
        return ((await this.cacheManager.cache.store.keys(keyPrefix + pattern)) || []).map(key => key.replace(keyPrefix, ''));
    }
    /**
     * 获取缓存
     * @param cacheName 缓存名称
     */
    async get(cacheName) {
        return await this.cacheManager.get(cacheName);
    }
    /**
     * 设置缓存
     * @param cacheName 缓存名称
     * @param value 缓存值
     * @param ttl 过期时间
     */
    async set(cacheName, value, ttl) {
        return await this.cacheManager.set(cacheName, value, { ttl: ttl });
    }
    /**
     * 删除缓存
     * @param cacheName 缓存名称
     * @param matchStart 是否匹配开头为缓存名称的所有key
     */
    async del(cacheName, matchStart = false) {
        if (matchStart) {
            const keys = await this.keys(cacheName + '*');
            for (const key of keys) {
                if (key.startsWith(cacheName)) {
                    await this.cacheManager.del(key);
                }
            }
        }
        else {
            await this.cacheManager.del(cacheName);
        }
    }
    /**
     * 清空缓存 redis会执行flushdb 慎用
     */
    async reset() {
        await this.cacheManager.reset();
    }
};
__decorate([
    (0, decorator_1.Inject)(),
    __metadata("design:type", cache_1.CacheManager)
], CacheModule.prototype, "cacheManager", void 0);
CacheModule = __decorate([
    (0, decorator_1.Provide)(),
    (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
], CacheModule);
exports.CacheModule = CacheModule;
//# sourceMappingURL=CacheModule.js.map