Skip to content

Commit

Permalink
feat: Add hash options to FileSysCache constructor
Browse files Browse the repository at this point in the history
This commit adds a `hash` property to the `IOptions` interface and `FileSysCache` constructor, allowing for hash customization in cache operations. It also updates the ESLint configuration to warn when named defaults are imported, and the documentation has been updated to reflect these changes. The module version in `package.json` has been bumped from 1.0.3 to 1.1.0.
  • Loading branch information
ndragun92 committed Mar 11, 2024
1 parent 1a1e6d1 commit 877f3e7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
rules: {
"@typescript-eslint/strict-boolean-expressions": "off",
"no-useless-catch": "off",
"@typescript-eslint/prefer-nullish-coalescing": "off"
"@typescript-eslint/prefer-nullish-coalescing": "off",
"import/no-named-default": "warn"
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { FileSysCache } from 'file-sys-cache'
const cache = new FileSysCache({
basePath: './.file-sys-cache', // Directory where cache will be stored
defaultTTL: 60, // 60 seconds expiration time
hash: 'sha256', // Hashing alorithm
debug: false, // Enabled debug mode
autoInvalidate: false ,// Auto invalidate files from file-system and delete expired files automatically without need of triggering .invalidate()
enableMonitoring: false // Enabled monitoring which exposes cache.monitoring.get(), cache.monitoring.reset()
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": "1.0.3",
"version": "1.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
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { default as FileSysCache } from './lib/file-sys-cache.ts'
// eslint-disable-next-line import/no-named-default
import { default as FileSysCache } from './lib/file-sys-cache'
import type { THashOptions } from './types/index.type'

export { type THashOptions, FileSysCache }
16 changes: 12 additions & 4 deletions src/lib/file-sys-cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import path from 'node:path'
import { promises as fsPromises, readdirSync, rmSync, unlinkSync, statSync } from 'node:fs'
import { type IArguments, type IGetArguments, type IOptions, type ISetArguments } from '../types/index.type.ts'
import {
type IArguments,
type IGetArguments,
type IOptions,
type ISetArguments,
type THashOptions
} from '../types/index.type.ts'
import { formatFileName } from '../utils/format.util.ts'
import FileSysCacheMonitoring, { monitoring } from './file-sys-cache-monitoring.ts'

Expand All @@ -17,22 +23,24 @@ const autoLog = {
export default class FileSysCache {
basePath: string
defaultTTL: number
hash?: THashOptions
debug: boolean
autoInvalidate: boolean
enableMonitoring: boolean
monitoringInstance?: FileSysCacheMonitoring

constructor ({ basePath, defaultTTL, debug, autoInvalidate, enableMonitoring }: IOptions) {
constructor ({ basePath, defaultTTL, hash, debug, autoInvalidate, enableMonitoring }: IOptions) {
this.basePath = basePath || './.file-sys-cache'
this.defaultTTL = defaultTTL || 60 // 60 seconds
this.hash = hash || 'sha256'
this.debug = debug || false
this.autoInvalidate = autoInvalidate || false
this.enableMonitoring = enableMonitoring || false
}

async set ({ fileName = '', key, payload, ttl = this.defaultTTL }: ISetArguments): Promise<string> {
const FILE_TTL = ttl
const FILE_NAME = formatFileName({ fileName, key })
const FILE_NAME = formatFileName({ fileName, key, hash: this.hash })
try {
// Construct the cache folder path
const cacheFolderPath = path.resolve(this.basePath)
Expand Down Expand Up @@ -83,7 +91,7 @@ export default class FileSysCache {
}
}

const FILE_NAME = formatFileName({ fileName, key })
const FILE_NAME = formatFileName({ fileName, key, hash: this.hash })
try {
// Construct the file path within the cache folder
const filePath = path.resolve(this.basePath, `${FILE_NAME}`)
Expand Down
1 change: 1 addition & 0 deletions src/types/index.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface IOptions {
basePath?: string
defaultTTL?: number
hash?: THashOptions
debug?: boolean
autoInvalidate?: boolean
enableMonitoring?: boolean
Expand Down
3 changes: 1 addition & 2 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { defineConfig } from 'tsup'

export default defineConfig({
entry: {
'file-sys-cache': 'src/index.ts',
'file-sys-cache.types': 'src/types/index.type.ts'
'file-sys-cache': 'src/index.ts'
},
format: ['cjs', 'esm'], // Build for commonJS and ESmodules
dts: true, // Generate declaration file (.d.ts)
Expand Down

0 comments on commit 877f3e7

Please sign in to comment.