• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

safe-global / safe-client-gateway / 10057609107

23 Jul 2024 10:54AM UTC coverage: 48.449% (-0.005%) from 48.454%
10057609107

Pull #1777

github

hectorgomezv
Implement CachedQueryResolver
Pull Request #1777: Extract getFromCacheOrExecuteAndCache utility function

437 of 2633 branches covered (16.6%)

Branch coverage included in aggregate %.

6 of 24 new or added lines in 2 files covered. (25.0%)

1 existing line in 1 file now uncovered.

4328 of 7202 relevant lines covered (60.09%)

13.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.74
/src/datasources/db/cached-query-resolver.ts
1
import {
16✔
2
  CacheService,
3
  ICacheService,
4
} from '@/datasources/cache/cache.service.interface';
5
import { CacheDir } from '@/datasources/cache/entities/cache-dir.entity';
6
import { ILoggingService, LoggingService } from '@/logging/logging.interface';
16✔
7
import { asError } from '@/logging/utils';
16✔
8
import {
16✔
9
  Inject,
10
  Injectable,
11
  InternalServerErrorException,
12
} from '@nestjs/common';
13
import postgres from 'postgres';
14

15
@Injectable()
16
// TODO: add/implement interface
17
export class CachedQueryResolver {
16✔
18
  constructor(
NEW
19
    @Inject(LoggingService) private readonly loggingService: ILoggingService,
×
NEW
20
    @Inject(CacheService) private readonly cacheService: ICacheService,
×
21
  ) {}
22

23
  /**
24
   * Returns the content from cache or executes the query and caches the result.
25
   * If the specified {@link CacheDir} is empty, the query is executed and the result is cached.
26
   * If the specified {@link CacheDir} is not empty, the pointed content is returned.
27
   *
28
   * @param cacheDir {@link CacheDir} to use for caching
29
   * @param query query to execute
30
   * @param ttl time to live for the cache
31
   * @returns content from cache or query result
32
   */
33
  async get<T extends postgres.MaybeRow[]>(args: {
34
    cacheDir: CacheDir;
35
    query: postgres.PendingQuery<T>;
36
    ttl: number;
37
  }): Promise<T> {
NEW
38
    const { key, field } = args.cacheDir;
×
NEW
39
    const cached = await this.cacheService.get(args.cacheDir);
×
NEW
40
    if (cached != null) {
×
NEW
41
      this.loggingService.debug({ type: 'cache_hit', key, field });
×
NEW
42
      return JSON.parse(cached);
×
43
    }
NEW
44
    this.loggingService.debug({ type: 'cache_miss', key, field });
×
45

46
    // log & hide database errors
NEW
47
    const result = await args.query.catch((e) => {
×
NEW
48
      this.loggingService.error(asError(e).message);
×
NEW
49
      throw new InternalServerErrorException();
×
50
    });
51

NEW
52
    if (result.count > 0) {
×
NEW
53
      await this.cacheService.set(
×
54
        args.cacheDir,
55
        JSON.stringify(result),
56
        args.ttl,
57
      );
58
    }
NEW
59
    return result;
×
60
  }
61
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc