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

egg-easy-process-cache

v0.3.0

Published

[![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][codecov-image]][codecov-url] [![David deps][david-image]][david-url] [![Known Vulnerabilities][snyk-image]][snyk-url] [![npm download][download-image]][down

Downloads

7

Readme

egg-easy-process-cache

NPM version [![build status][travis-image]][travis-url] Test coverage [![David deps][david-image]][david-url] Known Vulnerabilities npm download

依赖说明

依赖的 egg 版本

egg-easy-process-cache 版本 | egg 2.x --- | --- 2.x | 😁 0.x | ❌

开启插件

// config/plugin.js
exports.easyProcessCache = {
  enable: true,
  package: 'egg-easy-process-cache',
};

使用场景

  • 我在一些小型项目中,经常用到一些少量的数据,但又不想搭 redis,而且部署在生产的 Egg 项目是多进程模式的,所以缓存逻辑稍微复杂点,所以封装了该库,方便使用
  • 优点
    • 不依赖其他的库,实现 Egg 的多进程缓存
  • 缺点
    • 每个进程都有一份缓存,所以缓存的数据量只能少

app.js

export default class App {
  constructor(app) {
    this.app = app;
    this.init();
  }

  async init() {
    const { app } = this;
    const ctx = await this.app.createAnonymousContext();
    app.messenger.on(ctx.getProcessCacheEvent.ALL_WORKER_READY, async () => {
      // 监听了 ALL_WORKER_READY 事件,当所有子进程都准备好之后,你可以在这回调进行初始化数据
      // 该回调只会随机被某个子进程触发,而不是全部子进程都触发
      await ctx.service.qrcode.index.initStoreData();
    });
  }
}

更新缓存数据

function test() {
  // 当你要更新数据的时候,通知给 agent,并把数据带过去就行了
  // agent 会再通知全部子进程进行更新
  this.ctx.app.messenger.sendToAgent(this.ctx.getProcessCacheEvent.CACHE_COVER, data)
}

详细配置

config

config.easyProcessCache = {
    primaryKey: 'id',
    cacheField: [
      'id',
      'link'
    ], // 缓存的字段,不传就缓存全部
  };

Api

我在上下文添加了两个变量:

function test() {
  this.ctx.getProcessCache() // 返回缓存对象
  this.ctx.getProcessCacheEvent = {
    CACHE_COVER,
    CACHE_UPDATE,
    CACHE_PUSH,
    CACHE_CLEAR,
    CACHE_UPDATE_BY_KEY,
    WORKER_READY,
    ALL_WORKER_READY,
  } // getProcessCacheEvent 缓存了各个触发的事件
}

Cache 对象方法

| 方法名 | 参数 | 返回 | 说明 | | ----------- | ----------------------- | ------------------- | ------------------------------------ | | cover | data: Array | - | 覆盖缓存的数据 | | push | data: Object | - | 插入数据 | | removeByKey | val: any | - | 根据该值和 primaryKey 移除对应的对象 | | updateByKey | data: Object | - | 根据 primaryKey 更新对象的值 | | clear | - | - | 清空缓存数据 | | find | (key: string, val: any) | Object | undefined | 根据 key 和值查找对象 |

不建议通过 Cache 对象去更新缓存数据。要修改 Cache 对象的值,要用事件通知 agent 进行更新

预设的 agent 事件

| 事件名 | 参数 | 说明 | | ------------------- | ------------ | -------------------- | | CACHE_COVER | data: Array | 通知覆盖缓存数据 | | CACHE_PUSH | data: Object | 通知插入缓存数据 | | CACHE_UPDATE_BY_KEY | data: Object | 通知更新某个缓存数据 | | CACHE_REMOVE_BY_KEY | data: any | 通知移除某个数据 | | CACHE_CLEAR | - | 通知清空缓存数据 |

上面的通知,都是先更新 agent 进程的缓存数据,然后 agent 进程再通知其他子进程进行全覆盖操作

单元测试

提问交流

请到 egg issues 异步交流。

License

MIT