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

fs-cache-node

v0.0.1

Published

> This is a Cache class implemented in TypeScript that provides functionality for caching data into file/s with TTL.

Downloads

13

Readme

Cache Class

This is a Cache class implemented in TypeScript that provides functionality for caching data into file/s with TTL.

installing

yarn add fs-cache-node // npm i --save fs-cache-node

Usage

The Cache class can be used as follows:

import { Cache } from "fs-cache-node";

const cache = new Cache({ path: "/path/to/cache/directory" });
await cache.set("key", "value");
const value = await cache.get("key");

Methods

constructor(opt: CacheOption)

The constructor takes an object of type CacheOption as its parameter. It initializes a new instance of the Cache class with the specified options.

exists(key: Key): Promise<string | null>

This method checks if the specified key exists in the cache. If it does, the method returns the key. If it does not, the method returns null.

remember<T>(key: Key, seconds: number | undefined, callback: Callback<T>): Promise<T>

This method checks if the specified key exists in the cache. If it does, the method returns the cached value. If it does not, the method calls the specified callback function to get the value, caches it, and returns it.

rememberForever<T>(key: Key, callback: Callback<T>): Promise<T>

This method is similar to the remember method, but it caches the value forever.

clean(): Promise<void>

This method clears the cache.

remove(key: Key): Promise<Cache>

This method removes the specified key from the cache.

set(key: Key, value: unknown, config: SetOption = {}): Promise<void>

This method adds the specified key and value to the cache. It takes an optional configuration object that allows you to specify a time-to-live (TTL) value for the cached data.

get(key: Key, def?: any): Promise<any>

This method retrieves the value associated with the specified key from the cache. If the key does not exist in the cache, the method returns the default value specified in the second parameter.


Types

CacheOption

An interface that specifies the options that can be passed to the constructor of the Cache class.

interface CacheOption {
    path: string;
    ttl?: number; // seconds
    serializer?: Serializer;
    driver?: Driver;
}
Data<T>

An interface that specifies the shape of the cache data.

interface Data<T> {
    [key: string]: [T] | [T, number];
}
Serializer

Custom serializer like import serializer form 'v8' or import serializer from 'bson'

export interface Serializer {
    deserialize(raw: Buffer): Data<unknown>;
    serialize(data: unknown): Buffer | string;
}
Driver

File system driver

export interface Driver {
    read(filename: string): Promise<Buffer>;
    write(filename: string, content: Buffer | string): Promise<boolean>;
}
SetOption

An interface that specifies the configuration options that can be passed to the set method of the Cache class.

interface SetOption {
    ttl?: number;
}
Key

A type that represents the cache key. It can be either a string or a number.

type Key = string | number;
Callback<T>

A type that represents a callback function that can be used with the remember and rememberForever methods of the Cache class.

type Callback<T> = (cache: Cache) => PromiseLike<T> | T;

Cache Class (sync)

This is same but synchronous.

Usage

The Cache class can be used as follows:

import { Cache } from "fs-cache-node/sync";

const cache = new Cache({ path: "/path/to/cache/directory" });
cache.set("key", "value");
const value = cache.get("key");

Methods

constructor(opt: CacheOption)

The constructor takes an object of type CacheOption as its parameter. It initializes a new instance of the Cache class with the specified options.

exists(key: Key): string | null

This method checks if the specified key exists in the cache. If it does, the method returns the key. If it does not, the method returns null.

remember<T>(key: Key, seconds: number | undefined, callback: Callback<T>): T

This method checks if the specified key exists in the cache. If it does, the method returns the cached value. If it does not, the method calls the specified callback function to get the value, caches it, and returns it.

rememberForever<T>(key: Key, callback: Callback<T>): T

This method is similar to the remember method, but it caches the value forever.

clean(): void

This method clears the cache.

remove(key: Key): Cache

This method removes the specified key from the cache.

set(key: Key, value: unknown, config: SetOption = {}): void

This method adds the specified key and value to the cache. It takes an optional configuration object that allows you to specify a time-to-live (TTL) value for the cached data.

get(key: Key, def?: any): any

This method retrieves the value associated with the specified key from the cache. If the key does not exist in the cache, the method returns the default value specified in the second parameter.

Types

CacheOption

An interface that specifies the options that can be passed to the constructor of the Cache class.

interface CacheOption {
    path: string;
    ttl?: number;
    serializer?: Serializer;
}
Callback<T>

A type that represents a callback function that can be used with the remember and rememberForever methods of the Cache class.

type Callback<T> = (cache: Cache) => T;