Commit f85b3c7d authored by nanahira's avatar nanahira

rework with cache api

parent 866aa51f
Pipeline #6327 passed with stages
in 53 seconds
{ {
"name": "koishi-plugin-thesaurus", "name": "koishi-plugin-thesaurus",
"version": "1.0.0", "version": "1.0.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "koishi-plugin-thesaurus", "name": "koishi-plugin-thesaurus",
"version": "1.0.0", "version": "1.0.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"source-map-support": "^0.5.20" "source-map-support": "^0.5.20"
}, },
"devDependencies": { "devDependencies": {
"@types/lodash": "^4.14.175", "@types/lodash": "^4.14.175",
"@types/lru-cache": "^5.1.1",
"@types/node": "^16.11.1", "@types/node": "^16.11.1",
"@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0", "@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1", "eslint-plugin-prettier": "^3.4.1",
"koishi": "^4.0.0-alpha.9", "koishi": "^4.0.0-alpha.10",
"koishi-utils-schemagen": "^1.1.3", "koishi-utils-schemagen": "^1.1.3",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"lru-cache": "^6.0.0",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"proxy-agent": "^5.0.0", "proxy-agent": "^5.0.0",
"raw-loader": "^4.0.2", "raw-loader": "^4.0.2",
...@@ -214,22 +212,34 @@ ...@@ -214,22 +212,34 @@
} }
}, },
"node_modules/@koishijs/core": { "node_modules/@koishijs/core": {
"version": "4.0.0-alpha.9", "version": "4.0.0-alpha.10",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.0.0-alpha.9.tgz", "resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.0.0-alpha.10.tgz",
"integrity": "sha512-dVnK3+XGV0uee1hQo67gcYHx1Vjn7j1JD4Ztl884nlCmrGkF3Yglv2H24HSdZq1/0/SanfehVbF9g4YhvoLNgw==", "integrity": "sha512-VoCRV2pGyFsqxvvZnUz7a0/rvzwNoS4c61cdyKX05K3fjiHuzBWezk5vNwkgE5pvtCGmMXaOmMwSfazSZ/rLLQ==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"@koishijs/utils": "^5.0.0-alpha.5", "@koishijs/utils": "^5.0.0-alpha.6",
"fastest-levenshtein": "^1.0.12" "fastest-levenshtein": "^1.0.12"
}, },
"engines": { "engines": {
"node": ">=12.0.0" "node": ">=12.0.0"
} }
}, },
"node_modules/@koishijs/plugin-cache-lru": {
"version": "1.0.0-alpha.1",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-cache-lru/-/plugin-cache-lru-1.0.0-alpha.1.tgz",
"integrity": "sha512-GaLkOMMOy7djNSOT/eGHSUEu4kikmUjmxJIvOrAA/Y7oJAdXaTcY4gcZi5Vtie6NWXUIBssqqJmcJBgF/Bwwvw==",
"dev": true,
"dependencies": {
"lru-cache": "^6.0.0"
},
"peerDependencies": {
"koishi": "^4.0.0-alpha.10"
}
},
"node_modules/@koishijs/utils": { "node_modules/@koishijs/utils": {
"version": "5.0.0-alpha.5", "version": "5.0.0-alpha.6",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.0.0-alpha.5.tgz", "resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.0.0-alpha.6.tgz",
"integrity": "sha512-zYgsYu5ukD5xqtan1y/RlE198w1bYVngKo49wefERsmDLMplHUf/3R6qhmPad6gvYy5ALVmkTQLwX646yReMPg==", "integrity": "sha512-nvSZmDErNwAuTE+bVnVRn1s+Tt9ni7n5B6GbD2if7cNiLbWaJJkUgHLiiJaRRIhHJOmN/OEPaUwijf4j5LGXrg==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"supports-color": "^8.1.0" "supports-color": "^8.1.0"
...@@ -453,12 +463,6 @@ ...@@ -453,12 +463,6 @@
"integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==", "integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==",
"dev": true "dev": true
}, },
"node_modules/@types/lru-cache": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/@types/lru-cache/-/lru-cache-5.1.1.tgz",
"integrity": "sha512-ssE3Vlrys7sdIzs5LOxCzTVMsU7i9oa/IaW92wF32JFb3CVczqOkru2xspuKczHEbG3nvmPY7IFqVmGGHdNbYw==",
"dev": true
},
"node_modules/@types/mime": { "node_modules/@types/mime": {
"version": "1.3.2", "version": "1.3.2",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
...@@ -2699,20 +2703,22 @@ ...@@ -2699,20 +2703,22 @@
} }
}, },
"node_modules/koishi": { "node_modules/koishi": {
"version": "4.0.0-alpha.9", "version": "4.0.0-alpha.10",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.0.0-alpha.9.tgz", "resolved": "https://registry.npmjs.org/koishi/-/koishi-4.0.0-alpha.10.tgz",
"integrity": "sha512-rgGqktMxyM5XiuYPGNn5kCcsXpW0ceTT7mLYxyH5PBTIz1aF0FthzbaC+J0i5gLyDlHAC43lhND/ZxaudLS5Kg==", "integrity": "sha512-1d1Sztg4i6ob+voet0nbpaOt0RRkL7HJfTPQDAbzSZow49xFe4M0QyvyFJa3t96Mg60aQZCQZHrBkiAEQzPbYg==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"@koa/router": "^10.1.1", "@koa/router": "^10.1.1",
"@koishijs/core": "^4.0.0-alpha.9", "@koishijs/core": "^4.0.0-alpha.10",
"@koishijs/utils": "^5.0.0-alpha.5", "@koishijs/plugin-cache-lru": "^1.0.0-alpha.1",
"@koishijs/utils": "^5.0.0-alpha.6",
"@types/koa": "*", "@types/koa": "*",
"@types/koa__router": "*", "@types/koa__router": "*",
"@types/ws": "^7.4.7", "@types/ws": "^7.4.7",
"axios": "^0.21.4", "axios": "^0.21.4",
"koa": "^2.13.1", "koa": "^2.13.1",
"koa-bodyparser": "^4.3.0" "koa-bodyparser": "^4.3.0",
"proxy-agent": "^5.0.0"
}, },
"engines": { "engines": {
"node": ">=12.0.0" "node": ">=12.0.0"
...@@ -4489,19 +4495,28 @@ ...@@ -4489,19 +4495,28 @@
} }
}, },
"@koishijs/core": { "@koishijs/core": {
"version": "4.0.0-alpha.9", "version": "4.0.0-alpha.10",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.0.0-alpha.9.tgz", "resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.0.0-alpha.10.tgz",
"integrity": "sha512-dVnK3+XGV0uee1hQo67gcYHx1Vjn7j1JD4Ztl884nlCmrGkF3Yglv2H24HSdZq1/0/SanfehVbF9g4YhvoLNgw==", "integrity": "sha512-VoCRV2pGyFsqxvvZnUz7a0/rvzwNoS4c61cdyKX05K3fjiHuzBWezk5vNwkgE5pvtCGmMXaOmMwSfazSZ/rLLQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@koishijs/utils": "^5.0.0-alpha.5", "@koishijs/utils": "^5.0.0-alpha.6",
"fastest-levenshtein": "^1.0.12" "fastest-levenshtein": "^1.0.12"
} }
}, },
"@koishijs/plugin-cache-lru": {
"version": "1.0.0-alpha.1",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-cache-lru/-/plugin-cache-lru-1.0.0-alpha.1.tgz",
"integrity": "sha512-GaLkOMMOy7djNSOT/eGHSUEu4kikmUjmxJIvOrAA/Y7oJAdXaTcY4gcZi5Vtie6NWXUIBssqqJmcJBgF/Bwwvw==",
"dev": true,
"requires": {
"lru-cache": "^6.0.0"
}
},
"@koishijs/utils": { "@koishijs/utils": {
"version": "5.0.0-alpha.5", "version": "5.0.0-alpha.6",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.0.0-alpha.5.tgz", "resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.0.0-alpha.6.tgz",
"integrity": "sha512-zYgsYu5ukD5xqtan1y/RlE198w1bYVngKo49wefERsmDLMplHUf/3R6qhmPad6gvYy5ALVmkTQLwX646yReMPg==", "integrity": "sha512-nvSZmDErNwAuTE+bVnVRn1s+Tt9ni7n5B6GbD2if7cNiLbWaJJkUgHLiiJaRRIhHJOmN/OEPaUwijf4j5LGXrg==",
"dev": true, "dev": true,
"requires": { "requires": {
"supports-color": "^8.1.0" "supports-color": "^8.1.0"
...@@ -4709,12 +4724,6 @@ ...@@ -4709,12 +4724,6 @@
"integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==", "integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==",
"dev": true "dev": true
}, },
"@types/lru-cache": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/@types/lru-cache/-/lru-cache-5.1.1.tgz",
"integrity": "sha512-ssE3Vlrys7sdIzs5LOxCzTVMsU7i9oa/IaW92wF32JFb3CVczqOkru2xspuKczHEbG3nvmPY7IFqVmGGHdNbYw==",
"dev": true
},
"@types/mime": { "@types/mime": {
"version": "1.3.2", "version": "1.3.2",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
...@@ -6416,20 +6425,22 @@ ...@@ -6416,20 +6425,22 @@
} }
}, },
"koishi": { "koishi": {
"version": "4.0.0-alpha.9", "version": "4.0.0-alpha.10",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.0.0-alpha.9.tgz", "resolved": "https://registry.npmjs.org/koishi/-/koishi-4.0.0-alpha.10.tgz",
"integrity": "sha512-rgGqktMxyM5XiuYPGNn5kCcsXpW0ceTT7mLYxyH5PBTIz1aF0FthzbaC+J0i5gLyDlHAC43lhND/ZxaudLS5Kg==", "integrity": "sha512-1d1Sztg4i6ob+voet0nbpaOt0RRkL7HJfTPQDAbzSZow49xFe4M0QyvyFJa3t96Mg60aQZCQZHrBkiAEQzPbYg==",
"dev": true, "dev": true,
"requires": { "requires": {
"@koa/router": "^10.1.1", "@koa/router": "^10.1.1",
"@koishijs/core": "^4.0.0-alpha.9", "@koishijs/core": "^4.0.0-alpha.10",
"@koishijs/utils": "^5.0.0-alpha.5", "@koishijs/plugin-cache-lru": "^1.0.0-alpha.1",
"@koishijs/utils": "^5.0.0-alpha.6",
"@types/koa": "*", "@types/koa": "*",
"@types/koa__router": "*", "@types/koa__router": "*",
"@types/ws": "^7.4.7", "@types/ws": "^7.4.7",
"axios": "^0.21.4", "axios": "^0.21.4",
"koa": "^2.13.1", "koa": "^2.13.1",
"koa-bodyparser": "^4.3.0" "koa-bodyparser": "^4.3.0",
"proxy-agent": "^5.0.0"
} }
}, },
"koishi-utils-schemagen": { "koishi-utils-schemagen": {
......
{ {
"name": "koishi-plugin-thesaurus", "name": "koishi-plugin-thesaurus",
"version": "1.0.1", "version": "1.0.2",
"description": "Koishi.js chat plugin for https://github.com/Kyomotoi/AnimeThesaurus", "description": "Koishi.js chat plugin for https://github.com/Kyomotoi/AnimeThesaurus",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
...@@ -32,17 +32,15 @@ ...@@ -32,17 +32,15 @@
}, },
"devDependencies": { "devDependencies": {
"@types/lodash": "^4.14.175", "@types/lodash": "^4.14.175",
"@types/lru-cache": "^5.1.1",
"@types/node": "^16.11.1", "@types/node": "^16.11.1",
"@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0", "@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1", "eslint-plugin-prettier": "^3.4.1",
"koishi": "^4.0.0-alpha.9", "koishi": "^4.0.0-alpha.10",
"koishi-utils-schemagen": "^1.1.3", "koishi-utils-schemagen": "^1.1.3",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"lru-cache": "^6.0.0",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"proxy-agent": "^5.0.0", "proxy-agent": "^5.0.0",
"raw-loader": "^4.0.2", "raw-loader": "^4.0.2",
......
...@@ -3,6 +3,7 @@ import { Context } from 'koishi'; ...@@ -3,6 +3,7 @@ import { Context } from 'koishi';
import { MyPlugin } from './plugin'; import { MyPlugin } from './plugin';
import { MyPluginConfigLike } from './config'; import { MyPluginConfigLike } from './config';
export { MyPluginConfig } from './config'; export { MyPluginConfig } from './config';
export { ChatSession } from './plugin';
export const name = 'thesaurus'; export const name = 'thesaurus';
const plugin = new MyPlugin(); const plugin = new MyPlugin();
......
...@@ -4,14 +4,22 @@ import { MyPluginConfig, MyPluginConfigLike } from './config'; ...@@ -4,14 +4,22 @@ import { MyPluginConfig, MyPluginConfigLike } from './config';
import { schemaFromClass, schemaTransform } from 'koishi-utils-schemagen'; import { schemaFromClass, schemaTransform } from 'koishi-utils-schemagen';
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import { sleep } from 'koishi'; import { sleep } from 'koishi';
import LRUCache from 'lru-cache';
import _ from 'lodash'; import _ from 'lodash';
interface ChatSession { export interface ChatSession {
name: string; name: string;
playedWords: string[]; playedWords: string[];
} }
declare module 'koishi' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cache {
interface Tables {
thesaurusChatSession: ChatSession;
}
}
}
export class MyPlugin { export class MyPlugin {
private config: MyPluginConfig; private config: MyPluginConfig;
private ctx: Context; private ctx: Context;
...@@ -40,30 +48,34 @@ export class MyPlugin { ...@@ -40,30 +48,34 @@ export class MyPlugin {
return this.loadWords(); return this.loadWords();
} }
} }
// should be replaced with Koishi cache in future version
private lru: LRUCache<string, ChatSession>; private async getFromCache(sessionId: string) {
private async getFromCache(userId: string) { if (!sessionId) {
if (!userId) {
return; return;
} }
return this.lru.get(userId); return this.ctx.cache.get('thesaurusChatSession', sessionId);
} }
private async setToCache(userId: string, chatSession: ChatSession) { private async setToCache(sessionId: string, chatSession: ChatSession) {
if (!userId) { if (!sessionId) {
return; return;
} }
return this.lru.set(userId, chatSession, this.config.chatTimeout); return this.ctx.cache.set(
'thesaurusChatSession',
sessionId,
chatSession,
this.config.chatTimeout,
);
} }
private async clearCacheOf(userId: string) { private async clearCacheOf(sessionId: string) {
if (!userId) { if (!sessionId) {
return; return;
} }
return this.lru.del(userId); return this.ctx.cache.set('thesaurusChatSession', sessionId, null);
} }
private async replyWith( private async replyWith(
word: string, word: string,
userId: string, sessionId: string,
chatSession: ChatSession, chatSession: ChatSession,
) { ) {
if (!word) { if (!word) {
...@@ -73,7 +85,7 @@ export class MyPlugin { ...@@ -73,7 +85,7 @@ export class MyPlugin {
if (chatSession.playedWords.length > this.config.trackingLength) { if (chatSession.playedWords.length > this.config.trackingLength) {
chatSession.playedWords.shift(); chatSession.playedWords.shift();
} }
await this.setToCache(userId, chatSession); await this.setToCache(sessionId, chatSession);
return word.replace(/你/g, chatSession.name).trim(); return word.replace(/你/g, chatSession.name).trim();
} }
...@@ -94,7 +106,7 @@ export class MyPlugin { ...@@ -94,7 +106,7 @@ export class MyPlugin {
private async replyChat( private async replyChat(
word: string, word: string,
userId: string, sessionId: string,
chatSession: ChatSession, chatSession: ChatSession,
) { ) {
const matchingPatterns = Object.keys(this.wordData).filter((w) => const matchingPatterns = Object.keys(this.wordData).filter((w) =>
...@@ -110,7 +122,7 @@ export class MyPlugin { ...@@ -110,7 +122,7 @@ export class MyPlugin {
} }
return this.replyWith( return this.replyWith(
Random.pick(remainingAllWords), Random.pick(remainingAllWords),
userId, sessionId,
chatSession, chatSession,
); );
} }
...@@ -119,7 +131,7 @@ export class MyPlugin { ...@@ -119,7 +131,7 @@ export class MyPlugin {
matchingPatterns, matchingPatterns,
new Set<string>(chatSession.playedWords), new Set<string>(chatSession.playedWords),
) || this.tryPatterns(matchingPatterns, new Set<string>()); ) || this.tryPatterns(matchingPatterns, new Set<string>());
return this.replyWith(replyWord, userId, chatSession); return this.replyWith(replyWord, sessionId, chatSession);
} }
private formatName(name: string) { private formatName(name: string) {
if (name.match(/^[a-zA-Z0-9]/)) { if (name.match(/^[a-zA-Z0-9]/)) {
...@@ -138,23 +150,24 @@ export class MyPlugin { ...@@ -138,23 +150,24 @@ export class MyPlugin {
ctx.on('connect', () => { ctx.on('connect', () => {
if (ctx.database && this.config.useDatabase) this.useDatabase = true; if (ctx.database && this.config.useDatabase) this.useDatabase = true;
}); });
this.lru = new LRUCache<string, ChatSession>({ ctx.cache.table('thesaurusChatSession', {
maxAge: this.config.chatTimeout, maxAge: this.config.chatTimeout,
}); });
await this.loadWords(); await this.loadWords();
ctx.middleware(async (session, next) => { ctx.middleware(async (session, next) => {
const chatSession = await this.getFromCache(session.userId); const sessionId = `${session.platform}.${session.selfId}.${session.userId}`;
const chatSession = await this.getFromCache(sessionId);
if (chatSession) { if (chatSession) {
//ctx //ctx
// .logger('thesaurus') // .logger('thesaurus')
// .warn(`${session.userId} in chat ${JSON.stringify(chatSession)}`); // .warn(`${session.userId} in chat ${JSON.stringify(chatSession)}`);
if (session.content === 'quit') { if (session.content === 'quit') {
await this.clearCacheOf(session.userId); await this.clearCacheOf(sessionId);
return session.send('记得下次再找我聊喔~'); return session.send('记得下次再找我聊喔~');
} }
const reply = await this.replyChat( const reply = await this.replyChat(
session.content, session.content,
session.userId, sessionId,
chatSession, chatSession,
); );
return session.send(reply); return session.send(reply);
...@@ -170,7 +183,7 @@ export class MyPlugin { ...@@ -170,7 +183,7 @@ export class MyPlugin {
.userFields(['name']) .userFields(['name'])
.action(async (argv) => { .action(async (argv) => {
const { session, options } = argv; const { session, options } = argv;
const userId = session.userId; const sessionId = `${session.platform}.${session.selfId}.${session.userId}`;
let name = let name =
session.author?.nickname || session.author?.nickname ||
session.author?.username || session.author?.username ||
...@@ -187,7 +200,7 @@ export class MyPlugin { ...@@ -187,7 +200,7 @@ export class MyPlugin {
name = ''; name = '';
} }
const formattedName = this.formatName(name); const formattedName = this.formatName(name);
await this.setToCache(userId, { await this.setToCache(sessionId, {
name: formattedName, name: formattedName,
playedWords: [], playedWords: [],
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment