Skip to content

Commit

Permalink
Merge pull request #11 from ndragun92/version/2.1.0
Browse files Browse the repository at this point in the history
feat: Add 'files' method to FileSysCache class
  • Loading branch information
ndragun92 authored Mar 14, 2024
2 parents 62f9ab7 + 770e2c9 commit ecd7201
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ await cache.set({ fileName: 'myFileName', key: 'myUniqueKey', payload: myPayload
// Retrieve cached data by file name prefix and file name
const data = await cache.get({ fileName: 'myFileName', key: 'myUniqueKey' });

// Retrieve current list of files inside cache
const data = await cache.files();

// Flush cache by passing regex
await cache.flushByRegex('myString', 'myString2'); // Flush cache by regex match (single or multiple same matches)

Expand Down
38 changes: 38 additions & 0 deletions _docs/content/2.api/1.functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,44 @@ const data = await cache.get({ fileName: 'myFileName', key: 'myUniqueKey' });
```
---

### Get Files

::code-group

```ts [Code]
const files = cache.files()
```


```json [Response]
[
{
"name": "myFileNameFirst hash_132d085c88b595f1ef63d51afcc700028145f194732ae460aaec04f574c5c5de",
"size": {
"bytes": 75,
"megabytes": 0.00007152557373046875
},
"ttl": 60,
"expiration": 1710405034332,
"expires_in": 59.997
},
{
"name": "myFileNameSecond hash_132d085c88b595f1ef63d51afcc700028145f194732ae460aaec04f574c5c5de",
"size": {
"bytes": 75,
"megabytes": 0.00007152557373046875
},
"ttl": 60,
"expiration": 1710405034334,
"expires_in": 59.998
}
]
```

::

---

### Flush all

```ts
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "file-sys-cache",
"version": "2.0.1",
"version": "2.1.0",
"description": "A Node.js package providing efficient caching using the file system for storage.",
"type": "module",
"main": "./dist/file-sys-cache.cjs",
Expand Down
29 changes: 29 additions & 0 deletions src/lib/file-sys-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,35 @@ describe('FileSysCache', () => {
})
})

describe('files', () => {
const basePath = './.unit-file-sys-cache--files'
afterEach(() => {
// Delete cache folder after each test
rmSync(basePath, { recursive: true, force: true })
})
it('should return files', async () => {
const cache = new FileSysCache({ basePath })

await cache.set({ fileName: '1', key, payload })
await cache.set({ fileName: '2', key, payload })
const result = await cache.files()
const entry = result[0]
expect(Array.isArray(result)).toBeTruthy()
expect(entry.name).toBeDefined()
expect(entry.name).toContain('1 hash_')
expect(typeof entry.size).toBe('object')
expect(entry.size).toBeDefined()
expect(entry.size.bytes).toBeDefined()
expect(entry.size.bytes).toBe(75)
expect(entry.size.megabytes).toBeDefined()
expect(entry.size.megabytes).toBe(0.00007152557373046875)

expect(entry.ttl).toBe(60)
expect(entry.expiration).toBeDefined()
expect(entry.expires_in).toBeDefined()
})
})

describe('invalidate', () => {
const basePath = './.unit-file-sys-cache--invalidate'
afterAll(() => {
Expand Down
48 changes: 46 additions & 2 deletions src/lib/file-sys-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,55 @@ export default class FileSysCache {
}
}

async files (): Promise<Array<{
name: string
size: {
bytes: number
megabytes: number
}
ttl: number
expiration: number
expires_in: number | null
}>> {
const FILES: Array<{
name: string
size: {
bytes: number
megabytes: number
}
ttl: number
expiration: number
expires_in: number | null
}> = []
try {
const files = readdirSync(this.basePath)
for (const file of files) {
const stats = statSync(`${this.basePath}/${file}`)
try {
const { ttl, expiration, expiresIn } = (await this.validateFile(file)) || { ttl: 0, expiration: 0, expiresIn: 0 }
FILES.push({
name: file,
size: {
bytes: stats.size,
megabytes: stats.size / (1024 * 1024)
},
ttl,
expiration,
expires_in: expiresIn
})
} catch (_) {}
}
} catch (_) {
}
return FILES
}

async invalidate (): Promise<void> {
try {
const files = readdirSync(this.basePath)
for (const file of files) {
try {
const { expiresIn } = (await this.validateFile(file)) || { ttl: 0, expiresIn: 0 }
const { expiresIn } = (await this.validateFile(file)) || { expiresIn: 0 }
const invalid = (expiresIn || 0) <= 0
if (invalid) {
unlinkSync(`${this.basePath}/${file}`)
Expand All @@ -142,7 +185,7 @@ export default class FileSysCache {
}
}

private async validateFile (key: IArguments['key']): Promise<{ ttl: number, expiresIn: number | null } | undefined> {
private async validateFile (key: IArguments['key']): Promise<{ ttl: number, expiration: number, expiresIn: number | null } | undefined> {
try {
// Construct the file path within the cache folder
const filePath = path.resolve(this.basePath, `${key}`)
Expand All @@ -163,6 +206,7 @@ export default class FileSysCache {
// Return the payload data and TTL
return {
ttl: data.ttl,
expiration: data.expiration,
expiresIn
}
} catch (error: any) {
Expand Down

0 comments on commit ecd7201

Please sign in to comment.