@chengaoyuan/redis-cache

使用redis实现CacheProtocol

Usage no npm install needed!

<script type="module">
  import chengaoyuanRedisCache from 'https://cdn.skypack.dev/@chengaoyuan/redis-cache';
</script>

README

redis-cache

npm version install size NPM Downloads

Installation

$ npm install @chengaoyuan/redis-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));
})();