Skip to content

Commit

Permalink
feat: redisPrefix (#26398)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <[email protected]>
  • Loading branch information
monwolf and rarkins authored Jan 2, 2024
1 parent 320e80e commit c21bc85
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/usage/self-hosted-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,11 @@ Used as an alternative to `privateKeyOld`, if you want the key to be read from d

Override this object if you want to change the URLs that Renovate links to, e.g. if you have an internal forum for asking for help.

## redisPrefix

If this value is set then Renovate will prepend this string to the name of all Redis cache entries used in Renovate.
It's only used if `redisUrl` is configured.

## redisUrl

If this value is set then Renovate will use Redis for its global cache instead of the local file system.
Expand Down
7 changes: 7 additions & 0 deletions lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ const options: RenovateOptions[] = [
type: 'string',
globalOnly: true,
},
{
name: 'redisPrefix',
description: 'Key prefix for redis cache entries.',
stage: 'global',
type: 'string',
globalOnly: true,
},
{
name: 'baseDir',
description:
Expand Down
1 change: 1 addition & 0 deletions lib/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export interface GlobalOnlyConfig {
privateKeyPath?: string;
privateKeyPathOld?: string;
redisUrl?: string;
redisPrefix?: string;
repositories?: RenovateRepository[];
platform?: PlatformId;
endpoint?: string;
Expand Down
2 changes: 1 addition & 1 deletion lib/util/cache/package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function set(

export async function init(config: AllConfig): Promise<void> {
if (config.redisUrl) {
await redisCache.init(config.redisUrl);
await redisCache.init(config.redisUrl, config.redisPrefix);
cacheProxy = {
get: redisCache.get,
set: redisCache.set,
Expand Down
17 changes: 11 additions & 6 deletions lib/util/cache/package/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { logger } from '../../../logger';
import { compress, decompress } from '../../compress';

let client: ReturnType<typeof createClient> | undefined;
let rprefix: string | undefined;

function getKey(namespace: string, key: string): string {
return `${namespace}-${key}`;
return `${rprefix}${namespace}-${key}`;
}

export async function end(): Promise<void> {
Expand All @@ -20,7 +21,7 @@ export async function end(): Promise<void> {
}

async function rm(namespace: string, key: string): Promise<void> {
logger.trace({ namespace, key }, 'Removing cache entry');
logger.trace({ rprefix, namespace, key }, 'Removing cache entry');
await client?.del(getKey(namespace, key));
}

Expand All @@ -37,7 +38,7 @@ export async function get<T = never>(
const cachedValue = res && JSON.parse(res);
if (cachedValue) {
if (DateTime.local() < DateTime.fromISO(cachedValue.expiry)) {
logger.trace({ namespace, key }, 'Returning cached value');
logger.trace({ rprefix, namespace, key }, 'Returning cached value');
// istanbul ignore if
if (!cachedValue.compress) {
return cachedValue.value;
Expand All @@ -49,7 +50,7 @@ export async function get<T = never>(
await rm(namespace, key);
}
} catch (err) {
logger.trace({ namespace, key }, 'Cache miss');
logger.trace({ rprefix, namespace, key }, 'Cache miss');
}
return undefined;
}
Expand All @@ -60,7 +61,7 @@ export async function set(
value: unknown,
ttlMinutes = 5,
): Promise<void> {
logger.trace({ namespace, key, ttlMinutes }, 'Saving cached value');
logger.trace({ rprefix, namespace, key, ttlMinutes }, 'Saving cached value');

// Redis requires TTL to be integer, not float
const redisTTL = Math.floor(ttlMinutes * 60);
Expand All @@ -80,10 +81,14 @@ export async function set(
}
}

export async function init(url: string): Promise<void> {
export async function init(
url: string,
prefix: string | undefined,
): Promise<void> {
if (!url) {
return;
}
rprefix = prefix ?? '';
logger.debug('Redis cache init');
client = createClient({
url,
Expand Down

0 comments on commit c21bc85

Please sign in to comment.