From d5022252c44fa974da6314ce2936d72dfbc1ef2c Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sun, 31 Jan 2021 10:54:38 +0100
Subject: [PATCH] refactor: cdnjs use centralized caching

---
 lib/datasource/cache.ts       | 78 -----------------------------------
 lib/datasource/cdnjs/index.ts | 16 +++----
 2 files changed, 5 insertions(+), 89 deletions(-)
 delete mode 100644 lib/datasource/cache.ts

diff --git a/lib/datasource/cache.ts b/lib/datasource/cache.ts
deleted file mode 100644
index bab0cf1dbf..0000000000
--- a/lib/datasource/cache.ts
+++ /dev/null
@@ -1,78 +0,0 @@
-import { logger } from '../logger';
-import * as packageCache from '../util/cache/package';
-
-/**
- * Cache callback result which has to be returned by the `CacheCallback` function.
- */
-export interface CacheResult<TResult = unknown> {
-  /**
-   * The data which should be added to the cache
-   */
-  data: TResult;
-  /**
-   * `data` can only be cached if this is not `true`
-   */
-  isPrivate?: boolean;
-}
-
-/**
- * Simple helper type for defining the `CacheCallback` function return type
- */
-export type CachePromise<TResult = unknown> = Promise<CacheResult<TResult>>;
-
-/**
- * The callback function which is called on cache miss.
- */
-export type CacheCallback<TArg, TResult = unknown> = (
-  lookup: TArg
-) => CachePromise<TResult>;
-
-export type CacheConfig<TArg, TResult> = {
-  /**
-   * Datasource id
-   */
-  id: string;
-  /**
-   * Cache key
-   */
-  lookup: TArg;
-  /**
-   * Callback to use on cache miss to load result
-   */
-  cb: CacheCallback<TArg, TResult>;
-  /**
-   * Time to cache result in minutes
-   */
-  minutes?: number;
-};
-
-/**
- * Loads result from cache or from passed callback on cache miss.
- * @param param0 Cache config args
- */
-export async function cacheAble<TArg, TResult = unknown>({
-  id,
-  lookup,
-  cb,
-  minutes = 60,
-}: CacheConfig<TArg, TResult>): Promise<TResult> {
-  const cacheNamespace = `datasource-${id}`;
-  const cacheKey = JSON.stringify(lookup);
-  const cachedResult = await packageCache.get<TResult>(
-    cacheNamespace,
-    cacheKey
-  );
-  // istanbul ignore if
-  if (cachedResult) {
-    logger.trace({ id, lookup }, 'datasource cachedResult');
-    return cachedResult;
-  }
-  const { data, isPrivate } = await cb(lookup);
-  // istanbul ignore if
-  if (isPrivate) {
-    logger.trace({ id, lookup }, 'Skipping datasource cache for private data');
-  } else {
-    await packageCache.set(cacheNamespace, cacheKey, data, minutes);
-  }
-  return data;
-}
diff --git a/lib/datasource/cdnjs/index.ts b/lib/datasource/cdnjs/index.ts
index 58f912c645..944ece84da 100644
--- a/lib/datasource/cdnjs/index.ts
+++ b/lib/datasource/cdnjs/index.ts
@@ -1,9 +1,9 @@
 import { ExternalHostError } from '../../types/errors/external-host-error';
 import { Http } from '../../util/http';
-import { CachePromise, cacheAble } from '../cache';
 import { GetReleasesConfig, ReleaseResult } from '../common';
 
 export const id = 'cdnjs';
+export const caching = true;
 
 const http = new Http(id);
 
@@ -22,22 +22,16 @@ interface CdnjsResponse {
   assets?: CdnjsAsset[];
 }
 
-async function downloadLibrary(library: string): CachePromise<CdnjsResponse> {
-  const url = `https://api.cdnjs.com/libraries/${library}?fields=homepage,repository,assets`;
-  return { data: (await http.getJson<CdnjsResponse>(url)).body };
-}
-
 export async function getReleases({
   lookupName,
 }: GetReleasesConfig): Promise<ReleaseResult | null> {
   // Each library contains multiple assets, so we cache at the library level instead of per-asset
   const library = lookupName.split('/')[0];
+  const url = `https://api.cdnjs.com/libraries/${library}?fields=homepage,repository,assets`;
   try {
-    const { assets, homepage, repository } = await cacheAble({
-      id,
-      lookup: library,
-      cb: downloadLibrary,
-    });
+    const { assets, homepage, repository } = (
+      await http.getJson<CdnjsResponse>(url)
+    ).body;
     if (!assets) {
       return null;
     }
-- 
GitLab