Skip to content

Commit

Permalink
chore: apply fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
negezor committed Apr 10, 2024
1 parent 2c26185 commit e6f8e0f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 106 deletions.
4 changes: 2 additions & 2 deletions packages/cacher/src/adapters/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export interface IAdapterTouchOptions {
}

export interface IAdapterIncrementOptions {
key: string
value: number
key: string;
value: number;
}

export interface IAdapter {
Expand Down
16 changes: 8 additions & 8 deletions packages/cacher/src/adapters/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export class MemoryAdapter implements IAdapter {
}

public get(keys: string[]): Promise<(string | undefined)[]> {
return Promise.resolve(keys.map(key => (
this.storage.get(key) || undefined
)));
return Promise.resolve(keys.map(key => this.storage.get(key) || undefined));
}

public set(keys: IAdapterSetOptions[]): Promise<void> {
Expand All @@ -33,13 +31,15 @@ export class MemoryAdapter implements IAdapter {
}

public increment(keys: IAdapterIncrementOptions[]): Promise<(number | undefined)[]> {
return Promise.resolve(keys.map(({ key, value }) => {
const nextValue = Number(this.storage.get(key) || 0) + value;
return Promise.resolve(
keys.map(({ key, value }) => {
const nextValue = Number(this.storage.get(key) || 0) + value;

this.storage.set(key, String(nextValue));
this.storage.set(key, String(nextValue));

return nextValue;
}));
return nextValue;
}),
);
}

public delete(keys: string[]): Promise<void> {
Expand Down
35 changes: 10 additions & 25 deletions packages/cacher/src/adapters/union.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
IAdapter,
IAdapterIncrementOptions,
IAdapterSetOptions,
IAdapterTouchOptions,
} from './adapter';
import type { IAdapter, IAdapterIncrementOptions, IAdapterSetOptions, IAdapterTouchOptions } from './adapter';

export interface IUnionAdapterOptions {
adapters: [IAdapter, IAdapter];
Expand Down Expand Up @@ -36,9 +31,7 @@ export class UnionAdapter implements IAdapter {

const firstItems = await firstAdapter.get(keys);

const keysForSecond = keys.filter((item, index) => (
firstItems[index] === undefined
)) ;
const keysForSecond = keys.filter((item, index) => firstItems[index] === undefined);

if (keysForSecond.length === 0) {
return firstItems;
Expand Down Expand Up @@ -71,18 +64,16 @@ export class UnionAdapter implements IAdapter {

let index = -1;

return firstItems.map(item => (
return firstItems.map(item =>
item !== undefined
? item
// biome-ignore lint/suspicious/noAssignInExpressions: this is simple
: secondResult[index += 1]
));
: // biome-ignore lint/suspicious/noAssignInExpressions: this is simple
secondResult[(index += 1)],
);
}

public async set(keys: IAdapterSetOptions[]): Promise<void> {
await Promise.all(this.adapters.map(adapter => (
adapter.set(keys)
)));
await Promise.all(this.adapters.map(adapter => adapter.set(keys)));
}

public async increment(keys: IAdapterIncrementOptions[]): Promise<(number | undefined)[]> {
Expand All @@ -97,20 +88,14 @@ export class UnionAdapter implements IAdapter {
}

public async delete(keys: string[]): Promise<void> {
await Promise.all(this.adapters.map(adapter => (
adapter.delete(keys)
)));
await Promise.all(this.adapters.map(adapter => adapter.delete(keys)));
}

public async touch(keys: IAdapterTouchOptions[]): Promise<void> {
await Promise.all(this.adapters.map(adapter => (
adapter.touch(keys)
)));
await Promise.all(this.adapters.map(adapter => adapter.touch(keys)));
}

public async clear(namespace: string): Promise<void> {
await Promise.all(this.adapters.map(adapter => (
adapter.clear(namespace)
)));
await Promise.all(this.adapters.map(adapter => adapter.clear(namespace)));
}
}
64 changes: 30 additions & 34 deletions packages/cacher/src/cacher.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { type IAdapter, MemoryAdapter } from './adapters';
import { arraify } from './helpers';
import type {
ICacherOptions,
AllowArray,
Deserializer,
ICacherGetOptions,
ICacherSetOptions,
ICacherIncrementOptions,
ICacherOptions,
ICacherSetOptions,
ICacherTouchOptions,

Serializer,
Deserializer,

AllowArray,
} from './types';
import { arraify } from './helpers';

// biome-ignore lint/suspicious/noExplicitAny: by default any is simple for usage
export class Cacher<V = any> {
Expand Down Expand Up @@ -40,63 +38,61 @@ export class Cacher<V = any> {
}

public async get<K extends string = string, A extends string = string>(
rawKeys: AllowArray<ICacherGetOptions<K, A>>
rawKeys: AllowArray<ICacherGetOptions<K, A>>,
): Promise<Record<K, V | undefined> & Record<A, V | undefined>> {
const keys = arraify(rawKeys);

const rawValues = await this.adapter.get(
keys.map(item => this.getNamespaceKey(item.key))
);
const rawValues = await this.adapter.get(keys.map(item => this.getNamespaceKey(item.key)));

return Object.fromEntries(
rawValues.map((rawValue, i) => {
const key = keys[i].alias || keys[i].key;

const value = rawValue !== undefined
? this.deserializer(rawValue)
: undefined;
const value = rawValue !== undefined ? this.deserializer(rawValue) : undefined;

return [key, value];
})
}),
) as unknown as Record<K, V | undefined> & Record<A, V | undefined>;
}

public set(
rawKeys: AllowArray<ICacherSetOptions<V>>
): Promise<void> {
public set(rawKeys: AllowArray<ICacherSetOptions<V>>): Promise<void> {
const keys = arraify(rawKeys);

return this.adapter.set(keys.map(item => ({
key: this.getNamespaceKey(item.key),
value: this.serializer(item.value),
ttl: item.ttl,
})));
return this.adapter.set(
keys.map(item => ({
key: this.getNamespaceKey(item.key),
value: this.serializer(item.value),
ttl: item.ttl,
})),
);
}

public increment(rawKeys: AllowArray<ICacherIncrementOptions>): Promise<(number | undefined)[]> {
const keys = arraify(rawKeys);

return this.adapter.increment(keys.map(item => ({
key: this.getNamespaceKey(item.key),
value: item.value,
})));
return this.adapter.increment(
keys.map(item => ({
key: this.getNamespaceKey(item.key),
value: item.value,
})),
);
}

public async delete(rawKeys: AllowArray<string>): Promise<void> {
const keys = arraify(rawKeys);

return this.adapter.delete(keys.map(key => (
this.getNamespaceKey(key)
)));
return this.adapter.delete(keys.map(key => this.getNamespaceKey(key)));
}

public async touch(rawKeys: AllowArray<ICacherTouchOptions>): Promise<void> {
const keys = arraify(rawKeys);

return this.adapter.touch(keys.map(item => ({
key: this.getNamespaceKey(item.key),
ttl: item.ttl,
})));
return this.adapter.touch(
keys.map(item => ({
key: this.getNamespaceKey(item.key),
ttl: item.ttl,
})),
);
}

public clear(): Promise<void> {
Expand Down
6 changes: 1 addition & 5 deletions packages/cacher/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export const arraify = <T>(value: T | T[]): T[] => (
Array.isArray(value)
? value
: [value]
);
export const arraify = <T>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value]);
41 changes: 9 additions & 32 deletions packages/redis/src/redis.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import IORedis from 'ioredis';

import type {
IAdapter,
IAdapterIncrementOptions,
IAdapterSetOptions,
IAdapterTouchOptions,
} from '@cacher/cacher';
import type { IAdapter, IAdapterIncrementOptions, IAdapterSetOptions, IAdapterTouchOptions } from '@cacher/cacher';

export interface IRedisAdapterConnectionOptions {
hostname?: string;
Expand Down Expand Up @@ -43,11 +38,7 @@ export class RedisAdapter implements IAdapter {
public async get(keys: string[]): Promise<(string | undefined)[]> {
const values = await this.redis.mget(keys);

return values.map(value => (
value !== null
? value
: undefined
));
return values.map(value => (value !== null ? value : undefined));
}

public async set(keys: IAdapterSetOptions[]): Promise<void> {
Expand All @@ -67,15 +58,11 @@ export class RedisAdapter implements IAdapter {
const promises: Promise<unknown>[] = [];

if (mset.length !== 0) {
promises.push(
this.redis.mset(...mset)
);
promises.push(this.redis.mset(...mset));
}

if (ttlMset.length === 1) {
promises.push(
this.redis.set(...ttlMset[0])
);
promises.push(this.redis.set(...ttlMset[0]));
} else if (ttlMset.length !== 0) {
const pipeline = this.redis.multi();

Expand All @@ -100,14 +87,9 @@ export class RedisAdapter implements IAdapter {

const pipeline = this.redis.multi();

const promise = Promise.all(keys.map(({ key, value }) => (
this.redis.incrbyfloat(key, value)
)));
const promise = Promise.all(keys.map(({ key, value }) => this.redis.incrbyfloat(key, value)));

const { 1: result } = await Promise.all([
pipeline.exec(),
promise,
]);
const { 1: result } = await Promise.all([pipeline.exec(), promise]);

return result.map(Number);
}
Expand All @@ -127,14 +109,9 @@ export class RedisAdapter implements IAdapter {

const pipeline = this.redis.multi();

const promise = Promise.all(keys.map(({ key, ttl }) => (
this.redis.pexpire(key, ttl)
)));
const promise = Promise.all(keys.map(({ key, ttl }) => this.redis.pexpire(key, ttl)));

await Promise.all([
pipeline.exec(),
promise,
]);
await Promise.all([pipeline.exec(), promise]);
}

public async clear(namespace: string): Promise<void> {
Expand All @@ -143,7 +120,7 @@ export class RedisAdapter implements IAdapter {
});

for await (const keys of stream) {
await this.redis.del(...keys as string[]);
await this.redis.del(...(keys as string[]));
}
}
}

0 comments on commit e6f8e0f

Please sign in to comment.