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

nstarter-cache

v0.2.0

Published

nstarter 缓存插件

Downloads

21

Readme

nstarter-cache

nstarter-cache

注意:

  • nstarter-cache 提供的缓存方法依赖装饰器环境,使用前需要确认已开启装饰器支持。

安装

使用 npm 进行安装

npm install -S nstarter-cache

使用说明

首先,由 CacheManager 统一规定缓存管理器的基础封装形式。在一般情况下,仅需要简单扩展缓存的 「存」,「取」,「删」方法,即可实现基础的缓存管理对象封装。特殊场景下,可以将诸如 redis lua 脚本等复杂存储业务访问逻辑,封装至缓存管理对象中。另外,对于缓存的 key 生成规则,缓存生命周期策略等,均可以直接在 CacheManager 对象上作为扩展属性灵活定义。

import { AbstractCacheManager } from 'nstarter-cache';

export class DemoCacheManager extends AbstractCacheManager<any, string> {
    protected async _getCache(keyArg: string) {
        return demoCacheStore[this._getCacheKey(keyArg)];
    }

    protected async _putCache(keyArg: string, content: string) {
        demoCacheStore[this._getCacheKey(keyArg)] = content;
    }

    protected async _evictCache(keyArg: string) {
        delete demoCacheStore[this._getCacheKey(keyArg)];
    }

    protected _getCacheKey(keyArg: string) {
        return keyArg;
    }
}

在实际使用过程中,提供 @cacheGet, @cachePut, @cacheEvict 三个方法装饰器,通过与 CacheManager 对象组合,分别实现对于业务方法调用过程的 “带缓存读取”,“缓存强制更新”,“缓存删除” 逻辑。同时,额外提供一个参数装饰器 @cacheKey,用于标记调用方法中的缓存 key 生成参数。cacheKey 标记的调用方法入参并不限定仅支持 string 类型,允许为任意类型,并由 CacheManager 中的 _getCacheKey 方法处理。

import { cacheGet, cacheEvict, cacheKey, cachePut } from 'nstarter-cache';

export class DemoService {
    @cacheGet(demoCacheManager)
    public async getData(@cacheKey key: string) {
        return counter ++;
    }

    @cachePut(demoCacheManager)
    public async updateData(@cacheKey key: string, value: number) {
        counter = value;
        return value;
    }

    @cacheEvict(demoCacheManager)
    public async clearData(@cacheKey key: string) {
        counter = 0;
        return 0;
    }
}

复杂业务情况情况下,允许直接在业务方法中调用 CacheManager 对象,直接操作相关缓存方法。