import { Cache , Context , Dict , isNullable } from 'koishi' ; import LRU from 'lru-cache' ; class LruCache extends Cache { #store : Dict < LRU < string , any > > = Object . create ( null ) ; constructor ( ctx , private config ) { super ( ctx ) ; } start ( ) { } stop ( ) { } private prepare ( table ) { if ( this . #store [ table ] ) return ; const config = this . table ( table ) ; if ( ! config ) return ; this . #store [ table ] = new LRU ( { max : config . maxSize , maxAge : config . maxAge , } ) ; } async clear ( table ) { delete this . #store [ table ] ; } async get ( table , key ) { this . prepare ( table ) ; return this . #store [ table ] ?. get ( key ) ; } async set ( table , key , value , maxAge ? ) { if ( isNullable ( value ) ) { return ; } this . prepare ( table ) ; this . #store [ table ] ?. set ( key , value , maxAge ) ; } async del ( table , key ) { this . prepare ( table ) ; return this . #store [ table ] ?. del ( key ) ; } } namespace LruCache { export const name = 'cache-lru' ; export interface Config { } } export default LruCache ;