req-caching

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

req-caching - npm Package Compare versions

Comparing version

to
1.0.5

{
"name": "req-caching",
"version": "1.0.4",
"version": "1.0.5",
"type": "module",

@@ -19,3 +19,6 @@ "types": "lib",

"author": "Hrishikesh Vaze",
"license": "ISC"
"license": "ISC",
"dependencies": {
"jsonfile": "^6.1.0"
}
}

@@ -1,4 +0,4 @@

export type _Cache_type = "memory" | "cookie" | "localstorage"
export type CACHE_TYPE = "memory" | "cookie" | "localstorage"
export interface _Cache_opts {
export interface CACHE_OPTS {
maxAge?: number

@@ -8,12 +8,26 @@ strict?: boolean

export interface _Cache_value {
value: any
export interface CACHE_VALUE {
value: any | null
expires: number
}
export interface _Cache_ele {
export interface CACHE_ELEMENT {
key: string
value: CACHE_VALUE | null
seed: () => any
options?: _Cache_opts
options?: CACHE_OPTS
}
export interface _Cache_bunch extends Array<_Cache_ele> {}
export interface CACHE_BUNCH extends Array<CACHE_ELEMENT> {}
export const after_time: any = (t: any) => {
if (t.seconds) {
return new Date(Date.now() + t.seconds * 1000)
}
if (t.minutes) {
return new Date(Date.now() + t.minutes * 60 * 1000)
}
if (t.hours) {
return new Date(Date.now() + t.hours * 60 * 60 * 1000)
}
return new Date(Date.now())
}

@@ -1,3 +0,11 @@

import { _Cache_bunch, _Cache_opts, _Cache_value, _Cache_ele } from "./defined"
import {
CACHE_TYPE,
CACHE_BUNCH,
CACHE_OPTS,
CACHE_VALUE,
CACHE_ELEMENT,
} from "./defined"
import driver from "./driver"
/**

@@ -8,9 +16,14 @@ * Class Cache is heart of the library.

export default class Cache {
bunch: _Cache_bunch // bunch of calls
constructor() {
bunch: CACHE_BUNCH // bunch of calls
driver: any // driver type
constructor(stype: CACHE_TYPE = "memory") {
this.bunch = [] // lets bunch be an empty array
this.driver = driver[stype] // lets driver be a string
}
async add(key: string, seed: () => any, options: _Cache_opts) {
async add(key: string, seed: () => any, options: CACHE_OPTS) {
this.bunch.push({
key,
value: null,
seed,

@@ -20,2 +33,17 @@ options,

}
}
async get(key: string) {
let temp = await this.driver.get(key)
if (temp === null) {
let indexer: CACHE_ELEMENT | undefined = this.bunch.find(
(item) => item.key === key
)
if (indexer === undefined) {
return null
}
temp = indexer.seed()
await this.driver.save(key, temp, indexer.options?.maxAge)
}
return temp
}
}