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

@beautywe/plugin-cache

v1.0.0

Published

An beautywe plugin warp with @jerryc/super-cache

Downloads

10

Readme

@beautywe/plugin-cache

A beautywe plugin power by @jerryc/super-cache

CircleCI NPM Version NPM Downloads npm bundle size Coverage Status

安装

$ npm i @beautywe/plugin-cache

使用

import BeautyWe from '@beautywe/core';
import cache from '@beautywe/plugin-cache';

const app = new BeautyWe.BtApp();
app.use(cache({
    // ...
}));

set/get/remove

app.cache.set('name', 'jc');

// 输出:jc
app.cache.get('name').then(value => console.log(value));

app.cache.remove('name');

// 输出:undefined
app.cache.get('name').then(value => console.log(value));

Cache Adapters

可以针对某个 key 增加 adapter 来控制缓存的动作:

app.use(cache({
    adapters: [{
        key: 'name',
        data() {
            return API.fetch('xxx/name');
        }
    }]
}));

假设 API.fetch('xxx/name') 是请求服务器接口,返回数据:jc_from_server

那么:

app.cache.get('name').then((value) => {
    // value: 'jc_from_server'  
});

Adapter 的逻辑

设置 adapter 之后,get(key) 的默认逻辑是:

  1. 返回上一次的 key 缓存(如果缓存存在,则等待 adapter.data() 返回)
  2. 之后请求 adapter.data() ,然后刷新缓存
  3. 下一次 get(key),重复第一步。

假设 API.fetch('xxx/name') 会返回「第几次请求」的这个信息:jc_from_server_${request_times}

那么:

/**
 * 第一次请求
 * 请求前,name 缓存值:undefined
 * 请求后,name 缓存值:jc_from_server_1;
 * 返回:jc_from_server_1
 */
app.cache.get('name').then((value) => {
    // value: 'jc_from_server_1'  
});

/**
 * 第二次请求
 * 请求前,name 缓存值:jc_from_server_1
 * 请求后,name 缓存值:jc_from_server_2;
 * 返回:jc_from_server_1
 */
app.cache.get('name').then((value) => {
    // value: 'jc_from_server_1'  
});

/**
 * 第三次请求
 * 请求前,name 缓存值:jc_from_server_2
 * 请求后,name 缓存值:jc_from_server_3;
 * 返回:jc_from_server_2
 */
app.cache.get('name').then((value) => {
    // value: 'jc_from_server_2'  
});

Power by @jerryc/super-cache

beautywe-plugin-cache 是基于 @jerryc/super-cache 构建的,要更好的使用该插件,需要阅读 super-cache 的文档:@jerryc/super-cache

Plugin set/get/remove

app.cache.get/set/remove 是基于 SuperCache 的示例实现的,源码如下:

get(key) {
    return cache.get(key);
},
set(key, value) {
    return cache.set(key, value);
},
remove(key) {
    return cache.remove(key);
},

Options on use(cache(options))

除了 adapters ,options 的其他值,会传给的 new SuperCache(options) 中去:

app.use(cache({
    // adapters 配置,对应 `(new SuperCache).addAdapters(adapters[n])`
    adapters: [ ... ],

    // 自定义存储数据,默认存到内存,对应 `new SuperCache({ storage })`
    storage: { ... },

    // adapter 的全局配置,对应 `new SuperCache({ adapterOptions })`
    adapterOptions: { ... },

    // cache 的额外配置,对应 `new SuperCache({ extra })`
    extra: { ... },
}));

Get the instance of SuperCache

当然,你可以获取 SuperCache 的实例,然后调用所有 @jerryc/super-cache 的 API:

const cache = app.cache.getCacheInstance();

// Do all thing like on SuperCache
cache.addAdapters(...);
cache.get(...);
cache.set(...);
cache.remove(...);
// etc...

License

This project is licensed under the MIT license.

Copyright (c) JerryC Huang ([email protected])