New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@savla-dev/node-cache

Package Overview
Dependencies
Maintainers
2
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@savla-dev/node-cache

Simple caching for Node.js apps.

latest
npmnpm
Version
1.0.0
Version published
Maintainers
2
Created
Source

node-cache

Simple in-memory caching for Node.js apps.

Getting Started

Create a new instance of the cache, passing in the TTL for its items in seconds:

const cache = new Cache(900)

The following methods are exposed:

set(key: string, value: T): void
get(key: string): T | null
delete(key: string): void
// Clears all items from the cache
clear(): void

Example usage

import { Cache } from '@savla-dev/node-cache'

const cache = new Cache<unknown>(900) // 15 minutes TTL

async function fetchWithCache(url: string): Promise<any> {
  const cachedData = cache.get(url)
  if (cachedData) {
    console.log('Returning cached data')
    return cachedData
  }

  console.log('Fetching new data')

  let response: Response
  try {
    response = await fetch(url)
  } catch (e) {
    throw new Error(`Error during fetch: ${e}`)
  }

  try {
    const data = await response.json()
    cache.set(url, data)
    return data
  } catch (e) {
    console.log(`Error during response parsing: ${e}`)
  }
}

Keywords

cache

FAQs

Package last updated on 11 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts