cache: rm_cached_object

This commit is contained in:
lovebird 2025-04-06 11:14:24 +02:00
parent 8e638987b8
commit 7ec04bca12
3 changed files with 43 additions and 6 deletions

View File

@ -8,4 +8,5 @@ export declare const set_cached_object: (opts: any, namespace: string, data: any
export declare const get_cache_key: (path: string, opts?: any, namespace?: string) => Promise<any>;
export declare const get_cached: (path: string, opts?: any, namespace?: string) => Promise<any>;
export declare const get_cached_object: (opts?: any, namespace?: string) => Promise<any>;
export declare const rm_cached_object: (opts?: any, namespace?: string) => Promise<any>;
export declare const get_path_cached: (path: string, opts?: any, namespace?: string) => Promise<any>;

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
import { resolve, join } from 'node:path'
import * as ssri from 'ssri'
import { get as cache_get, put as cache_put } from 'cacache'
import { get as cache_get, put as cache_put, rm as cache_rm } from 'cacache'
import { sync as exists } from '@polymech/fs/exists'
import { sync as mkdir } from '@polymech/fs/dir'
import { isString, isObject, isArray } from '@polymech/core/primitives'
@ -57,6 +57,7 @@ export const set_cached = async (path: string, opts: any = {}, namespace: string
export const set_cached_object = async (opts: any = {}, namespace: string = "nons", data: any, metadata: any = {}) => {
const c_integrity = object_hash(opts, namespace)
const c_dir = cache_path(namespace)
if (!exists(c_dir)) {
mkdir(c_dir)
}
@ -125,17 +126,34 @@ export const get_cached_object = async (opts: any = {}, namespace: string = "non
} catch (e) {
logger.error(e)
}
if (data && isString(data)) {
try {
data = JSON.parse(data)
} catch (e) {
logger.error(`Error parsing cached object: ${e}`, opts)
logger.error(`Error parsing cached object: ${e.message}`)
}
}
return data
}
export const rm_cached_object = async (opts: any = {}, namespace: string = "nons") => {
const c_integrity = object_hash(opts, namespace)
const c_dir = cache_path(namespace)
if (!exists(c_dir)) {
mkdir(c_dir)
}
let data: any
try {
const cached = await cache_get.info(c_dir, c_integrity)
if (cached) {
await cache_rm(c_dir, c_integrity)
}
} catch (e) {
logger.error(e)
}
return data
}
export const get_path_cached = async (path: string, opts: any = {}, namespace: string = "nons") => {
const c_integrity = file_hash(path, opts, namespace)
const c_dir = cache_path(namespace)