File

src/cache/cache.service.ts

Index

Properties
Methods

Constructor

constructor(configService: ConfigService)
Parameters :
Name Type Optional
configService ConfigService No

Methods

Public Async add
add(key: string, data: T, TTL: number)
Type parameters :
  • T
Parameters :
Name Type Optional Default value
key string No
data T No
TTL number No 0
Returns : Promise<boolean>
Public Async remove
remove(key: string)
Parameters :
Name Type Optional
key string No
Returns : Promise<boolean>

Properties

Private Readonly httpService
Type : HttpService
Default value : new HttpService()
Private Readonly logger
Default value : new Logger(CacheService.name)
import { Injectable, Logger } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class CacheService {
  private readonly httpService: HttpService = new HttpService(); //DI wont fucking work with this stupid cunt
  private readonly logger = new Logger(CacheService.name);
  constructor(private readonly configService: ConfigService) {}

  public async add<T>(key: string, data: T, TTL = 0): Promise<boolean> {
    this.logger.log(`Caching object: ${key}`);
    const resp = this.httpService.post('https://cache.file.glass/cache', {
      akey: this.configService.get('CACHE_AKEY'),
      key,
      data: data,
      cacheFor: TTL,
    });
    const parsed = await resp.toPromise();
    this.logger.log(`Caching worker response: ${parsed.data}`);
    return parsed.data == 'CACHED';
  }

  public async remove(key: string): Promise<boolean> {
    this.logger.log(`Removing object from cache: ${key}`);
    const resp = this.httpService.post('https://cache.file.glass/removecache', {
      akey: this.configService.get('CACHE_AKEY'),
      key,
    });
    const parsed = await resp.toPromise();
    this.logger.log(`Caching worker response: ${parsed.data}`);
    return parsed.data == 'CACHE_REMOVED';
  }
}

results matching ""

    No results matching ""