import {Inject, Injectable} from "@nestjs/common";
import {Cache, CACHE_MANAGER} from "@nestjs/cache-manager";

@Injectable()
export class XCacheService {
    constructor(
        @Inject(CACHE_MANAGER) private cacheManager: Cache
    ) {
    }

    async delCachedKey(key: string) {
        return await this.cacheManager.del(key); // Retrieve data
    }

    async getCachedData<T>(key: string) {
        return await this.cacheManager.get<T>(key); // Retrieve data
    }

    async setCachedKey(key: string, data, ttl_by_sec = 1000) {
        await this.cacheManager.set(key, data, ttl_by_sec * 1000); // Save to Redis
        return data;
    }

    async setCacheTweetUrlById(tweetId, tweetUrl) {
        await this.setCachedKey(`x_tweetId:${tweetId}`, tweetUrl, 24*3600);
        return {tweetId, tweetUrl};
    }

    async getCacheTweetUrlById(tweetId) {
        return await this.getCachedData(`x_tweetId:${tweetId}`) as string;
    }

    async incrCountCollectNewsapi() {
        //this.cacheManager.c
    }

    async setTwitterRefreshToken(token: string) {
        // return this.setCachedKey('twitter_refresh_token', token, 30 * 24 * 1000);
    }

    async getTwitterRefreshToken() {
        // return this.setCachedKey('twitter_refresh_token', token, 30 * 24 * 1000);
    }
}
