Skip to content

Commit

Permalink
refactor: more repository changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zackpollard committed Aug 12, 2024
1 parent 9f09c0f commit 331fcc5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
2 changes: 1 addition & 1 deletion tiles/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function handleRequest(
});
}

const memCacheRepository = new MemCacheRepository();
const memCacheRepository = new MemCacheRepository(globalThis.memCache);
const kvRepository = new CloudflareKVRepository(env.KV);
const storageRepository = new R2StorageRepository(env.BUCKET, env.PMTILES_FILE_NAME);

Expand Down
20 changes: 5 additions & 15 deletions tiles/src/pmtiles/pmtiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,12 @@ import {
const HEADER_SIZE_BYTES = 127;

export class PMTiles {
source: IStorageRepository;
memCache: IMemCacheRepository;
kvCache: IKeyValueRepository;
ctx: ExecutionContext;

private constructor(
source: IStorageRepository,
memCache: IMemCacheRepository,
kvCache: IKeyValueRepository,
ctx: ExecutionContext,
) {
this.source = source;
this.memCache = memCache;
this.kvCache = kvCache;
this.ctx = ctx;
}
private source: IStorageRepository,
private memCache: IMemCacheRepository,
private kvCache: IKeyValueRepository,
private ctx: ExecutionContext,
) {}

static async init(
source: IStorageRepository,
Expand Down
29 changes: 12 additions & 17 deletions tiles/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { IKeyValueRepository, IMemCacheRepository, IStorageRepository } from './
import { Metrics } from './monitor';

export class CloudflareKVRepository implements IKeyValueRepository {
private KV: KVNamespace;
constructor(KV: KVNamespace) {
this.KV = KV;
}
constructor(private KV: KVNamespace) {}

async put(key: string, value: string): Promise<void> {
await Metrics.getMetrics().monitorAsyncFunction({ name: 'kv-put-value', extraTags: { key } }, this.KV.put)(
Expand Down Expand Up @@ -34,32 +31,30 @@ export class CloudflareKVRepository implements IKeyValueRepository {

/* eslint-disable no-var */
declare global {
var headerCache: Map<string, unknown>;
var memCache: Map<string, unknown>;
}
/* eslint-enable no-var */

if (!globalThis.headerCache) {
globalThis.headerCache = new Map<string, unknown>();
if (!globalThis.memCache) {
globalThis.memCache = new Map<string, unknown>();
}

export class MemCacheRepository implements IMemCacheRepository {
constructor(private globalCache: Map<string, unknown>) {}
set<T>(key: string, value: T): void {
headerCache.set(key, value);
this.globalCache.set(key, value);
}

get<T>(key: string): T | undefined {
return headerCache.get(key) as T;
return this.globalCache.get(key) as T;
}
}

export class R2StorageRepository implements IStorageRepository {
private bucket: R2Bucket;
private readonly fileName: string;

constructor(bucket: R2Bucket, filename: string) {
this.fileName = filename;
this.bucket = bucket;
}
constructor(
private bucket: R2Bucket,
private fileName: string,
) {}

getFileName(): string {
return this.fileName;
Expand All @@ -76,6 +71,6 @@ export class R2StorageRepository implements IStorageRepository {

const o = resp as R2ObjectBody;

return await o.arrayBuffer();
return o.arrayBuffer();
}
}

0 comments on commit 331fcc5

Please sign in to comment.