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

@unode/egg-recachegoose

v1.0.2

Published

recachegoose for egg.js

Downloads

8

Readme

egg-recachegoose

本插件用于eggjs生态,结合redismongoose,实现对查询结果的缓存。

依赖的 egg 版本

egg-validate 版本 | egg 1.x --- | --- 1.x | 😁 0.x | ❌

依赖的插件

您需要在项目中安装并启用如下插件,本插件才能正常工作。

  • egg-redis
  • egg-mongoose

开启插件

// config/plugin.js
exports.recachegoose = {
  enable: true,
  package: '@unode/egg-recachegoose'
}

详细配置

请到 config/config.default.js 查看详细配置项说明。

exports.recachegoose = {
    // 缓存的时间,单位秒
    ttl: 60,
    // 在redis中的前缀,为了避免和其他键造成冲突
    keyPrefix: 'recachegoose'
}

使用方法

  1. 对查询结果使用cache()方法进行缓存,如不调用此方法,则默认不缓存。
// controller/home.js
const {ctx} = this
// 对查询结果缓存100秒
const res = await ctx.model.User.find({}).cache(100)
  1. 由于缓存缓存了结果,下一次通用的查询条件将会优先从缓存中取出结果,会造成一些问题,比如过程中有新的记录产生,或者被删除,修改等,那么缓存中的结果就不会是最新的,所以插件提供了一个方法,在表中有数据变动时,会直接清空该表中相关的缓存结果。
// app/model/user.js
module.exports = (app) => {
    const mongoose = app.mongoose
    const Schema = mongoose.Schema
    const modelName = 'user'

    const table = new Schema({
        userName: {
            type: String,
            default: ''
        }
    })

    // 通过clear参数(数组),对指定的操作发生时进行清空本表的缓存
    // modelName参数必填,用于表明本表的名称
    table.plugin(app.recachegoose.autoClearCache, { modelName, clear: ['update', 'delete', 'add'] })

    return mongoose.model(modelName, table, modelName)
}

当然,您也可以在service中通过调用插件的方法,手动清空缓存,如下:

// 清空某个表的缓存,下为user表
app.recachegoose.clearCache('user:*')

// 清空某个已知名称的缓存键,一般用不到
app.recachegoose.clearCache('xxx')

License

MIT