"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
const cacheManager = require("cache-manager");
const fsStore = require("cache-manager-fs");
const thenify = require("thenify");
const pnpm_logger_1 = require("pnpm-logger");
const cacheLogger = pnpm_logger_1.default('cache');
function createCache(opts) {
    const cacheManagerOpts = {
        store: fsStore,
        ttl: opts.ttl,
        maxsize: 1000 * 1000 * 1000,
        path: opts.path,
        preventfill: opts.ttl === 0,
    };
    const diskCache = cacheManager.caching(cacheManagerOpts);
    const getCache = thenify(diskCache.get.bind(diskCache));
    const setCache = thenify(diskCache.set.bind(diskCache));
    return {
        set: (key, value) => setCache(key, value, cacheManagerOpts),
        get: function (id) {
            return __awaiter(this, void 0, void 0, function* () {
                try {
                    return yield getCache(id, cacheManagerOpts);
                }
                catch (err) {
                    cacheLogger.error('cache', id);
                    return null; // ignore errors. The data can be requested.
                }
            });
        },
    };
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = createCache;
//# sourceMappingURL=createCache.js.map