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

layered-cache-loader

v0.0.4

Published

Add cache layers based on dataloader.

Downloads

13

Readme

layered-cache-loader

NPM version

这是什么?

layered-cache-loader是一个基于dataloader的多级缓存解决方案。

怎么使用?

// 创建内存缓存层
const layer1 = new MemoryLayer<string, string>();
// 创建Redis缓存层
const layer2 = new RedisLayer<string, string>({
  uri: 'redis://localhost:6379',
  ttl: 10000,
});
// 创建数据源
const finalMap = new Map<string, string>([
  ['foo', 'bar'],
  ['foo2', 'hahaha'],
  ['hello', 'world'],
]);
// 创建loader,并逐个use缓存层(先use的层距离最近,所以建议越快的缓存层越先use)
const layeredCacheLoader = new LayeredCacheLoader<string, string>()
  .use(layer1)
  .use(layer2)
  .final(async keys => keys.map(key => finalMap.get(key) ?? new Error('Value not found.'))); // 未命中任何缓存的情况下,从final loader读取数据
const results = await layeredCacheLoader.loadMany(['foo', 'foo2', 'hello']); // 使用dataloader的数据读取方案,详情可以查看dataloader文档
assert(results[0] === 'bar');
assert(results[1] === 'hahaha');
assert(results[2] === 'world');

API定义

LayeredCacheLoader<K, V>

Kkey值的类型;

V: value值的类型;

ICacheLayer<K, V>

export interface ICacheLayer<K, V> {
  batchGet(keys: readonly K[]): Promise<Array<V | Error>>;

  batchSet(map: Map<K, V>): void;
}

自定义缓存层按照ICacheLayer<K, V>定义

LayeredCacheLoader().use(layer: ICacheLayer<K, V>): this

使用use来配置缓存层,先配置的层距离结果返回最近,建议速度越快的缓存层越先配置。

LayeredCacheLoader().final(batchLoadFn: BatchLoadFn<K, V>): this

使用final来配置最终数据源,当key没有命中任何缓存层的情况下,会从final loader读取数据。例:在batchLoadFn中从MongodbMysql读取原始数据。

final可多次调用,以最后一次调用为准。

LayeredCacheLoader().load(key: K): Promise<V>

dataloader.load

LayeredCacheLoader().loadMany(keys: K[]): Promise<Array<(V | Error)>>

dataloader.loadMany

MemoryLayer<K, V, CK = K>

Kkey值的类型;

V: value值的类型;

CK: 缓存的key的类型;

IMemoryLayerOptions<K, V, CK>

export interface IMemoryLayerOptions<K, V, CK> {
  ttl?: number; // 缓存存活时间(毫秒),默认为Infinity,即永不过期

  keyFn?(key: K): CK; // key到cacheKey的转换函数
}

MemoryLayer(options: IMemoryLayerOptions<K, V, CK> = {})

构造一个基于内存的缓存层对象。

readonly MemoryLayer().map: TTLMap<K, V>

内存缓存层用于存储数据的TTLMap对象,TTLMap见后文。

RedisLayer<K, V>

Kkey值的类型;

V: value值的类型;

IRedisLayerOptions<K, V>

export interface IRedisLayerOptions<K, V> {
  uri: string; // redis连接uri
  keyPrefix?: string; // 存储到redis中的key的附加前缀(命名空间),默认为'cacheloader'
  ttl?: number; // 缓存存货时间(毫秒),默认为Infinity,即永不过期

  keyFn?(key: K): string;  // key到cacheKey的转换函数

  serializer?(value: V): string; // 序列化函数,默认为JSON.stringify

  deserializer?(data: string): V | Error; // 反序列化函数,默认为JSON.parse
}

RedisLayer(options: IRedisLayerOptions<K, V>)

构造一个基于Redis的缓存层对象。

readonly RedisLayer().redis: IORedis.Redis

redis缓存层内部使用的Redis对象。

TTLMap<K, V> extends Map<K, V>

Kkey值的类型;

V: value值的类型;

带有ttl(tive to live)Map

TTLMap().setTTL(key: K, value: V, ttl: number): this

Map().set,第3个参数ttl设置该key的存活时间。