UNPKG

@tomisakae/syosetu-api

Version:

Enterprise-grade Fastify TypeScript API for Syosetu.com data extraction using official API and web scraping. Run instantly with 'npx @tomisakae/syosetu-api'

86 lines 2.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cacheManager = exports.CacheManager = void 0; const node_cache_1 = __importDefault(require("node-cache")); const config_1 = require("@/config"); const logger_1 = require("./logger"); const logger = (0, logger_1.createChildLogger)('Cache'); class CacheManager { metadataCache; contentCache; constructor() { this.metadataCache = new node_cache_1.default({ stdTTL: config_1.cacheConfig.metadata.ttl, checkperiod: 120, }); this.contentCache = new node_cache_1.default({ stdTTL: config_1.cacheConfig.content.ttl, checkperiod: 300, }); this.metadataCache.on('set', (key, _value) => { logger.debug(`Metadata cached: ${key}`); }); this.contentCache.on('set', (key, _value) => { logger.debug(`Content cached: ${key}`); }); this.metadataCache.on('expired', (key, _value) => { logger.debug(`Metadata cache expired: ${key}`); }); this.contentCache.on('expired', (key, _value) => { logger.debug(`Content cache expired: ${key}`); }); } getMetadata(key) { return this.metadataCache.get(key); } setMetadata(key, value, ttl) { return ttl ? this.metadataCache.set(key, value, ttl) : this.metadataCache.set(key, value); } deleteMetadata(key) { return this.metadataCache.del(key); } getContent(key) { return this.contentCache.get(key); } setContent(key, value, ttl) { return ttl ? this.contentCache.set(key, value, ttl) : this.contentCache.set(key, value); } deleteContent(key) { return this.contentCache.del(key); } flushAll() { this.metadataCache.flushAll(); this.contentCache.flushAll(); logger.info('All caches flushed'); } getStats() { return { metadata: this.metadataCache.getStats(), content: this.contentCache.getStats(), }; } static generateMetadataKey(ncode) { return `metadata_${ncode.toLowerCase()}`; } static generateContentKey(ncode, chapter) { return `content_${ncode.toLowerCase()}_${chapter}`; } static generateSearchKey(keyword, options) { const optionsStr = JSON.stringify(options); return `search_${keyword}_${Buffer.from(optionsStr).toString('base64')}`; } static generateRankingKey(options) { const optionsStr = JSON.stringify(options); return `ranking_${Buffer.from(optionsStr).toString('base64')}`; } } exports.CacheManager = CacheManager; exports.cacheManager = new CacheManager(); //# sourceMappingURL=cache.js.map