Skip to content

Commit

Permalink
feat: add clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
inaie-makerx committed May 27, 2024
1 parent 5cbe4c1 commit b8adba6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ export interface ObjectCache {
* @param mimeType Optional mime type of the data; default = `application/json` or `application/octet-stream` depending on if the data is binary or JSON.
*/
putBinary(cacheKey: string, data: Uint8Array, mimeType?: string): Promise<void>

/**
* Clear the cache value for te given cache key
* @param cacheKey A unique key that identifies the cached value
*/
clearCache(cacheKey: string): void
}
32 changes: 32 additions & 0 deletions src/fileSystemObjectCache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, beforeEach, test } from '@jest/globals'
import path from 'node:path'
import * as fs from 'node:fs/promises'
import * as fsSync from 'node:fs'
import { FileSystemObjectCache } from './fileSystemObjectCache'

const testDir = path.join(__dirname, '..', '.cache')
Expand Down Expand Up @@ -30,6 +31,37 @@ describe('FileSystemObjectCache', () => {
`)
})

test('Clear json file cache', async () => {
const cache = new FileSystemObjectCache(testDir, true)

await cache.put('test', { test: 1 })

const data = await fs.readFile(path.join(testDir, 'test.json'), { encoding: 'utf-8' })
expect(data).toMatchInlineSnapshot(`
"{
"test": 1
}"
`)

await cache.clearCache('test')
const fileExist = fsSync.existsSync(path.join(testDir, 'test.json'))
expect(fileExist).toBe(false)
})

test('Clear binary cache', async () => {
const cache = new FileSystemObjectCache(testDir, true)
const fileData = Uint8Array.from(atob('test'), (c) => c.charCodeAt(0))

await cache.put('test', fileData)

const data = await fs.readFile(path.join(testDir, 'test'))
expect(data.compare(fileData)).toBe(0)

await cache.clearCache('test')
const fileExist = fsSync.existsSync(path.join(testDir, 'test'))
expect(fileExist).toBe(false)
})

test("Get an object from cache if it didn't exist", async () => {
const cache = new FileSystemObjectCache(testDir, true)

Expand Down
11 changes: 11 additions & 0 deletions src/fileSystemObjectCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ export class FileSystemObjectCache implements ObjectCache {
}
}

/**
* Clear the cache value for te given cache key
* @param cacheKey A unique key that identifies the cached value
*/
async clearCache(cacheKey: string): Promise<void> {
if (fsSync.existsSync(path.join(this.cacheDirectory, cacheKey))) await fs.unlink(path.join(this.cacheDirectory, cacheKey))

if (fsSync.existsSync(path.join(this.cacheDirectory, `${cacheKey}.json`)))
await fs.unlink(path.join(this.cacheDirectory, `${cacheKey}.json`))
}

/** Adds the given value to the cache for the given cache key
* @param cacheKey A unique key that identifies the cached value
* @param data The data to cache
Expand Down
31 changes: 30 additions & 1 deletion src/s3ObjectCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
import { DeleteObjectCommand, GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
import { BinaryCacheOptions, BinaryWithMetadata, CacheOptions, ObjectCache } from './cache'
import * as path from 'node:path'

Expand All @@ -22,6 +22,35 @@ export class S3ObjectCache implements ObjectCache {
this.keyPrefix = keyPrefix
}

/**
* Clear the cache value for te given cache key
* @param cacheKey A unique key that identifies the cached value
*/
async clearCache(cacheKey: string): Promise<void> {
const deleteFile = async (key: string) => {
const bucketAndKey = {
Bucket: this.bucket,
Key: key,
}

const fileExist = await this.s3Client
.send(new HeadObjectCommand(bucketAndKey))
.then(() => true)
.catch(() => false)

if (fileExist) {
await this.s3Client.send(
new DeleteObjectCommand({
...bucketAndKey,
}),
)
}
}

deleteFile(this.keyPrefix ? path.join(this.keyPrefix, `${cacheKey}.gz`) : `${cacheKey}.gz`)
deleteFile(this.keyPrefix ? path.join(this.keyPrefix, `${cacheKey}.json.gz`) : `${cacheKey}.json.gz`)
}

/** Adds the given value to the cache for the given cache key
* @param cacheKey A unique key that identifies the cached value
* @param data The data to cache
Expand Down

0 comments on commit b8adba6

Please sign in to comment.