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

@quikc/core

v0.1.11

Published

This package contains the core logic and interfaces for the caching library.

Downloads

6

Readme

@quikc/core - Core package for Quikc a NodeJS caching library

Table of contents

Introduction

@quikc/core is a Node.js caching library that supports multiple cache providers and pluggable lock providers. It is designed to be framework-agnostic and can be used in any Node.js application.

Installation

@quikc/core is not framwork specific and can be used in any Node.js application. you can install it by running the following command:

npm install @quikc/core
# or
yarn add @quikc/core

Available strategies

You can use one of the following strategies:

  • memory - In-memory cache
  • fs - File system cache
  • redis - Redis cache

For the redis strategy, the cache provider utilizes ioredis under the hood. You can install it by running the following command:

npm install ioredis
# or
yarn add ioredis

Creating a cache instance

Memory Cache provider

You can use the memory cache provider by passing the memory strategy to the createStore function or by creating a new instance of the MemoryCacheProvider class.

import { createStore, MemoryCacheProvider } from '@quikc/core';

// Using the createStore function
const cache = createStore('memory');

// Using the MemoryCacheProvider class
const cache = new MemoryCacheProvider();

File system Cache provider

You can use the file system cache provider by passing the fs strategy to the createStore function or by creating a new instance of the FileSystemCacheProvider class.

import { createStore, FileSystemCacheProvider } from '@quikc/core';

// Using the createStore function
const cache = createStore('fs',{
  // The path to the cache directory
  cachePath: './cache'
});

// Using the FileSystemCacheProvider class
const cache = new FileSystemCacheProvider('./cache');

Redis Cache provider

You can use the redis cache provider by passing the redis strategy to the createStore function or by creating a new instance of the RedisCacheProvider class.

import { createStore, RedisCacheProvider } from '@quikc/core';
import { Redis } from 'ioredis';

// Using the createStore function
const cache = createStore('redis',{
  // The redis client instance
  redisClient: new Redis({
    host: 'localhost',
    port: 6379
  })
});

// Using the RedisCacheProvider class
const cache = new RedisCacheProvider(new Redis({
  host: 'localhost',
  port: 6379
}));

Using the cache instance

The cache instance exposes the following methods:

  • get(key: string): Promise<unknown | undefined> - Gets the cached value associated with the specified key.
  • set(key: string, value: unknown, options?: CacheOptions): Promise<void> - Stores a value in the cache using the specified key.
  • del(key: string): Promise<void> - Removes the cache entry associated with the specified key.
  • clear(): Promise<void> - Clears all cached entries from the cache.
  • getStats(): CacheStats - Retrieves cache statistics including hits, misses, and hit rate.
  • setLockProvider(lockProvider: ILockProvider): void - Sets a lock provider to handle cache locking.
  • getDependentKeys(key: string): Promise<string[] | undefined> - Gets the dependent keys for the specified key.
  • delDependentKeys(keys: string[]): Promise<void> - Deletes the dependent keys for the specified key.

CacheOptions is an interface that defines the options that can be passed to the set method of a cache provider. Here is the list of available options:

  • ttl: The time-to-live (TTL) for the cached value, in seconds.
  • dependencies: An array of keys for other cached values that the current value depends on. If any of these dependent keys are deleted or updated, the current value will also be invalidated.
  • priority: A number indicating the priority of the cached value, relative to other cached values. Higher-priority values will be retained in cache longer than lower-priority values, even if their TTLs have expired.
  • lockTimeout: The maximum amount of time to wait for a lock when setting the cached value. If this timeout is exceeded, the set operation will fail.

And the CacheEntry interface defines the structure of a cached value:

interface CacheEntry {
  value: unknown;
  expiresAt: number;
  dependencies?: string[];
  priority?: number;
  locked?: number;
}
  • value: The value of the cache entry.
  • expiresAt: The time at which the cache entry expires.
  • dependencies: An optional array of cache entry dependencies.
  • priority: An optional priority value for the cache entry.
  • locked: An optional timeout value for acquiring a lock on the cache entry.

Lock providers

Locks are used to prevent multiple concurrent writes to the same cache key, which can result in inconsistent or invalid cached data. Quikc supports pluggable lock providers that can be used to configure how locks are managed.

Memory Lock provider

You can use the memory lock provider by passing the memory strategy to the createLock function or by creating a new instance of the MemoryLockProvider class.

import { createLock, MemoryLockProvider } from '@quikc/core';

// Using the createLock function
const lock = createLock('memory');

// Using the MemoryLockProvider class
const lock = new MemoryLockProvider();

Redis Lock provider

You can use the redis lock provider by passing the redis strategy to the createLock function or by creating a new instance of the RedisLockProvider class.

import { createLock, RedisLockProvider } from '@quikc/core';
import { Redis } from 'ioredis';

// Using the createLock function
const lock = createLock('redis',{
  // The redis client instance
  redisClient: new Redis({
    host: 'localhost',
    port: 6379
  })
});

// Using the RedisLockProvider class
const lock = new RedisLockProvider(new Redis({
  host: 'localhost',
  port: 6379
}));

File system Lock provider

You can use the file system lock provider by passing the fs strategy to the createLock function or by creating a new instance of the FileSystemLockProvider class.

import { createLock, FileSystemLockProvider } from '@quikc/core';

// Using the createLock function
const lock = createLock('fs',{
  // The path to the lock directory
  lockPath: './lock'
});

// Using the FileSystemLockProvider class
const lock = new FileSystemLockProvider('./lock');

Using the lock instance

The lock instance exposes the following methods:

  • acquireLock(key: string, timeout?: number): Promise<boolean>: Acquires a lock on the specified cache entry.
  • releaseLock(key: string): Promise<void>: Releases the lock on the specified cache entry.
  • clearLocks(): Promise<void>: Clears all locks. This method is intended for testing purposes only.
  • getLock(key: string): Promise<boolean | undefined>: Returns a Promise that resolves to true if the lock is successfully acquired, or false otherwise.

and you can use the setLockProvider method of the cache instance to set the lock provider:

import { createStore, createLock, MemoryCacheProvider, MemoryLockProvider } from '@quikc/core';

const cache = createStore('memory');

// Using the createLock function
const lock = createLock('memory');

// Using the MemoryLockProvider class
const lock = new MemoryLockProvider();

cache.setLockProvider(lock);

Contributing

Contributions are welcome! Please read the contributing guide for more information.

License

MIT