Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@criipto/cache

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@criipto/cache

Cache implementations for Node.js/TypeScript

latest
Source
npmnpm
Version
1.1.2
Version published
Weekly downloads
77
-42.96%
Maintainers
0
Weekly downloads
 
Created
Source

node-cache

Cache implementations for Node.js/TypeScript

Policies

All the cache implementations allow you to inject policy decision callbacks.

Policies help you define how the cache should behave when it is updating or hold stale data.

import {CacheUpdatePolicy, CachePendingPolicy} from '@criipto/cache';

CacheUpdatePolicy

  • CacheUpdatePolicy.UPDATE: Trigger update of value for cache key
  • CacheUpdatePolicy.DONT_UPDATE: No-op, keep current value

CachePendingPolicy

  • CachePendingPolicy.WAIT: Blocks the read while waiting for cache refresh to finish
  • CachePendingPolicy.STALE: Return stale cache data while the cache updates

Memory backed cache

Example


import {CacheUpdatePolicy, CachePendingPolicy, memoryCache} from '@criipto/cache';

const metadataCache = memoryCache({
  updatePolicy: (url, {lastUpdatedAt}) => {
    // Update metadata if data is more than an hour old
    if (lastUpdatedAt.valueOf() < (Date.now() - 60 * 60 * 1000)) {
      return CacheUpdatePolicy.UPDATE;
    }
    return CacheUpdatePolicy.DONT_UPDATE;
  },
  pendingPolicy: () => CachePendingPolicy.STALE,
  refresh: async (url: URL) => {
    return await fetch(url);
  }
});

Storage backed cache

Example


import {CacheUpdatePolicy, CachePendingPolicy, storedCache} from '@criipto/cache';

const metadataCache = storedCache({
  updatePolicy: (url, {lastUpdatedAt}) => {
    // Update metadata if data is more than an hour old
    if (lastUpdatedAt.valueOf() < (Date.now() - 60 * 60 * 1000)) {
      return CacheUpdatePolicy.UPDATE;
    }
    return CacheUpdatePolicy.DONT_UPDATE;
  },
  pendingPolicy: () => CachePendingPolicy.STALE,
  refresh: async (url: URL) => {
    const response = await fetch(url);
    if (response.status !== 200) throw new Error('Not 200');
    return response.json() as Promise<{metadata: string}>
  },
  storage: {
    getItem: async (url) => {
      return await fetchFromStorageImplementation(url);
    },
    setItem: async (url, data) => {
      return await upsertInStorageImplementation(url, data);
    }
  }
});

FAQs

Package last updated on 18 Mar 2025

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