@chengaoyuan/cache
TypeScript icon, indicating that this package has built-in type declarations

2.0.3 • Public • Published

cache

npm version install size NPM Downloads

Installation

$ npm install @chengaoyuan/cache

Usage

import { RedisClient } from "redis";
import RedisCache from "@chengaoyuan/redis-cache";
import { CacheInit, Cacheable, CachePut, CacheEvict } from "@chengaoyuan/cache";
CacheInit(
    new RedisCache(
        new RedisClient({
            host: "127.0.0.1",
            port: 6379,
            password: "password",
            db: 10
        })
    )
);

class Test {
    datas: { [key: string]: any };
    constructor() {
        this.datas = {};
    }

    @Cacheable({
        cache: "Test",
        key: "id",
        expire: 30,
        condition: "id >= 10"
    })
    async getData(id: number) {
        return this.datas[id];
    }

    @CacheEvict({
        cache: "Test",
        key: "id"
    })
    async setData(id: number, data: any) {
        this.datas[id] = data;
    }

    @CachePut({
        cache: "Test",
        key: "id",
        expire: 30
    })
    async getDataEx(id: number) {
        return this.datas[id];
    }
}

const t = new Test();
(async function() {
    console.log(await t.getData(10));
    console.log(await t.getData(10));
    t.setData(10, { id: 123, name: "gg" });
    console.log(await t.getData(10));
    console.log(await t.getData(10));
    console.log(await t.getDataEx(10));
})();

Testing

$ npm test

Readme

Keywords

Package Sidebar

Install

npm i @chengaoyuan/cache

Weekly Downloads

5

Version

2.0.3

License

MIT

Unpacked Size

24.5 kB

Total Files

9

Last publish

Collaborators

  • chengaoyuan