2020-10-31 11:45:33 +00:00
|
|
|
import { Collection } from "../utils/collection.ts";
|
|
|
|
import { Client } from "./client.ts";
|
2020-11-01 11:22:09 +00:00
|
|
|
import { connect, Redis, RedisConnectOptions } from "https://denopkg.com/keroxp/deno-redis/mod.ts";
|
2020-10-31 11:45:33 +00:00
|
|
|
|
|
|
|
export interface ICacheAdapter {
|
|
|
|
client: Client
|
2020-11-01 11:22:09 +00:00
|
|
|
get: (cacheName: string, key: string) => Promise<any> | any
|
|
|
|
set: (cacheName: string, key: string, value: any) => Promise<any> | any
|
|
|
|
delete: (cacheName: string, key: string) => Promise<boolean> | boolean
|
|
|
|
array: (cacheName: string) => void | any[] | Promise<any[] | void>
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DefaultCacheAdapter implements ICacheAdapter {
|
|
|
|
client: Client
|
|
|
|
data: {
|
|
|
|
[name: string]: Collection<string, any>
|
|
|
|
} = {}
|
|
|
|
|
|
|
|
constructor(client: Client) {
|
|
|
|
this.client = client
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:22:09 +00:00
|
|
|
async get(cacheName: string, key: string) {
|
2020-10-31 13:00:33 +00:00
|
|
|
const cache = this.data[cacheName]
|
2020-10-31 11:45:33 +00:00
|
|
|
if (!cache) return;
|
|
|
|
return cache.get(key)
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:22:09 +00:00
|
|
|
async set(cacheName: string, key: string, value: any) {
|
2020-10-31 11:45:33 +00:00
|
|
|
let cache = this.data[cacheName]
|
|
|
|
if (!cache) {
|
|
|
|
this.data[cacheName] = new Collection()
|
|
|
|
cache = this.data[cacheName]
|
|
|
|
}
|
|
|
|
cache.set(key, value)
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:22:09 +00:00
|
|
|
async delete(cacheName: string, key: string) {
|
2020-10-31 13:00:33 +00:00
|
|
|
const cache = this.data[cacheName]
|
2020-10-31 11:45:33 +00:00
|
|
|
if (!cache) return false
|
|
|
|
return cache.delete(key)
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:22:09 +00:00
|
|
|
async array(cacheName: string) {
|
2020-10-31 13:00:33 +00:00
|
|
|
const cache = this.data[cacheName]
|
2020-10-31 11:45:33 +00:00
|
|
|
if (!cache) return
|
|
|
|
return cache.array()
|
|
|
|
}
|
2020-11-01 11:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class RedisCacheAdapter implements ICacheAdapter {
|
|
|
|
client: Client
|
|
|
|
_redis: Promise<Redis>
|
|
|
|
redis?: Redis
|
|
|
|
ready: boolean = false
|
|
|
|
|
|
|
|
constructor(client: Client, options: RedisConnectOptions) {
|
|
|
|
this.client = client
|
|
|
|
this._redis = connect(options)
|
|
|
|
this._redis.then(redis => {
|
|
|
|
this.redis = redis
|
|
|
|
this.ready = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async _checkReady() {
|
|
|
|
if(!this.ready) return await this._redis;
|
|
|
|
else return;
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(cacheName: string, key: string) {
|
|
|
|
await this._checkReady()
|
|
|
|
let cache = await this.redis?.hget(cacheName, key)
|
|
|
|
if(!cache) return
|
|
|
|
try {
|
|
|
|
return JSON.parse(cache as string)
|
|
|
|
} catch(e) { return cache }
|
|
|
|
}
|
|
|
|
|
|
|
|
async set(cacheName: string, key: string, value: any) {
|
|
|
|
await this._checkReady()
|
|
|
|
return await this.redis?.hset(cacheName, key, typeof value === "object" ? JSON.stringify(value) : value)
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(cacheName: string, key: string) {
|
|
|
|
await this._checkReady()
|
|
|
|
let exists = await this.redis?.hexists(cacheName, key)
|
|
|
|
if(!exists) return false
|
|
|
|
await this.redis?.hdel(cacheName, key)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
async array(cacheName: string) {
|
|
|
|
await this._checkReady()
|
|
|
|
let data = await this.redis?.hvals(cacheName)
|
|
|
|
return data?.map((e: string) => JSON.parse(e))
|
|
|
|
}
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|