npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

extra-disk-cache

v0.12.0

Published

A disk-based persistent cache.

Downloads

7

Readme

extra-disk-cache

A disk-based persistent cache.

Install

npm install --save extra-disk-cache
# or
yarn add extra-disk-cache

Usage

import { DiskCache } from 'extra-disk-cache'
import ms from 'ms'

const cache = await DiskCache.create('/tmp/cache')
cache.set('key', Buffer.from('value'), ms('1h'))
const value = cache.get('key')?.toString()

API

DiskCache

class DiskCache {
  static create(filename?: string): Promise<DiskCache>

  close(): void
  has(key: string): boolean
  get(key: string): Buffer | undefined
  getWithMetadata(key: string): {
    value: Buffer
    updatedAt: number
    timeToLive: number | null
  } | undefined
  set(
    key: string
  , value: Buffer
    /**
     * `timeToLive > 0`: items will expire after `timeToLive` milliseconds.
     * `timeToLive = 0`: items will expire immediately.
     * `timeToLive = null`: items will not expire.
     */
  , timeToLive: number | null = null
  ): void
  delete(key: string): void
  clear(): void
  keys(): IterableIterator<string>
}

DiskCacheView

interface IKeyConverter<T> {
  toString: (value: T) => string
  fromString: (value: string) => T | undefined
}

interface IValueConverter<T> {
  toBuffer: (value: T) => Buffer
  fromBuffer: (value: Buffer) => T
}

class DiskCacheView<K, V> {
  constructor(
    cache: DiskCache | DiskCacheWithCache
  , keyConverter: IKeyConverter<K>
  , valueConverter: IValueConverter<V>
  )

  has(key: K): boolean
  get(key: K): V | undefined
  getWithMetadata(key: K): {
    value: V
    updatedAt: number
    timeToLive: number | null
  } | undefined
  set(
    key: K
  , value: V
    /**
     * `timeToLive > 0`: items will expire after `timeToLive` milliseconds.
     * `timeToLive = 0`: items will expire immediately.
     * `timeToLive = null`: items will not expire.
     */
  , timeToLive: number | null = null
  ): void
  delete(key: K): void
  clear(): void
  keys(): IterableIterator<K>
}

DiskCacheAsyncView

interface IKeyAsyncConverter<T> {
  toString: (value: T) => Awaitable<string>
  fromString: (value: string) => Awaitable<T | undefined>
}

interface IValueAsyncConverter<T> {
  toBuffer: (value: T) => Awaitable<Buffer>
  fromBuffer: (value: Buffer) => Awaitable<T>
}

class DiskCacheAsyncView<K, V> {
  constructor(
    cache: DiskCache | DiskCacheWithCache
  , keyConverter: IKeyAsyncConverter<K>
  , valueConverter: IValueAsyncConverter<V>
  )

  has(key: K): Promise<boolean>
  get(key: K): Promise<V | undefined>

  getWithMetadata(key: K): Promise<{
    value: V
    updatedAt: number
    timeToLive: number | null
  } | undefined>
  set(
    key: K
  , value: V
    /**
     * `timeToLive > 0`: items will expire after `timeToLive` milliseconds.
     * `timeToLive = 0`: items will expire immediately.
     * `timeToLive = null`: items will not expire.
     */
  , timeToLive: number | null = null
  ): Promise<void>
  delete(key: K): Promise<void>
  clear(): void
  keys(): AsyncIterableIterator<K>
}

DiskCacheWithCache

interface ICache {
  set(
    key: string
  , value:
    | {
        value: Buffer
        updatedAt: number
        timeToLive: number | null
      }
    | false
  , timeToLive?: number
  ): void

  get(key: string):
  | {
      value: Buffer
      updatedAt: number
      timeToLive: number | null
    }
  | false
  | undefined

  delete(key: string): void
  clear(): void
}

class DiskCacheWithCache {
  constructor(diskCache: DiskCache, memoryCache: ICache)

  close(): void
  has(key: string): boolean
  get(key: string): Buffer | undefined
  getWithMetadata(key: string): {
    value: Buffer
    updatedAt: number
    timeToLive: number | null
  } | undefined
  set(key: string, value: Buffer, timeToLive: number | null = null): void
  delete(key: string): void
  clear(): void
  keys(): IterableIterator<string>
}

Converters

PassthroughKeyConverter

class PassthroughKeyConverter implements IKeyConverter<string>, IKeyAsyncConverter<string>

PassthroughValueConverter

class PassthroughValueConverter implements IValueConverter<Buffer>, IValueAsyncConverter<Buffer>

JSONKeyConverter

class JSONKeyConverter<T> implements IKeyConverter<T>, IKeyAsyncConverter<T>

JSONValueConverter

class JSONValueConverter<T> implements IValueConverter<T>, IValueAsyncConverter<T> {
  constructor(encoding: BufferEncoding = 'utf-8')
}

IndexKeyConverter

class IndexKeyConverter implements IKeyConverter<number>, IKeyAsyncConverter<number> {
  constructor(radix: number = 10)
}

MessagePackValueConverter

class MessagePackValueConverter<T> implements IValueConverter<T>, IValueAsyncConverter<T>

LZ4ValueConverter

class LZ4ValueConverter<T> implements IValueConverter<T>, IValueAsyncConverter<T> {
  constructor(valueConverter: IValueConverter<T>)
}

LZ4ValueAsyncConverter

class LZ4ValueAsyncConverter<T> implements IValueAsyncConverter<T> {
  constructor(valueConverter: IValueConverter<T> | IValueAsyncConverter<T>)

  toBuffer(value: T): Promise<Buffer>
  fromBuffer(value: Buffer): Promise<T>
}

ZstandardValueAsyncConverter

class ZstandardValueAsyncConverter<T> implements IValueAsyncConverter<T> {
  constructor(
    valueConverter: IValueConverter<T> | IValueAsyncConverter<T>
  , level: number
  )

  toBuffer(value: T): Promise<Buffer>
  fromBuffer(value: Buffer): Promise<T>
}

PrefixKeyConverter

export class PrefixKeyConverter<T> implements IKeyConverter<T>, IKeyAsyncConverter<T> {
  constructor(
    keyConverter: IKeyConverter<T>
  , prefix: string
  )

  toString(value: T): string
  fromString(value: string): T | undefined
}

PrefixKeyAsyncConverter

class PrefixKeyAsyncConverter<T> implements IKeyAsyncConverter<T> {
  constructor(
    keyConverter: IKeyConverter<T> | IKeyAsyncConverter<T>
  , prefix: string
  )

  toString(value: T): Promise<string>
  fromString(value: string): Promise<T | undefined>
}