Commit 46b86a9e authored by nanahira's avatar nanahira

first

parent 9fce67a5
Pipeline #6332 canceled with stages
in 11 seconds
webpack.config.js
dist/*
build/*
\ No newline at end of file
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
/build
/output
/dest
/config.yaml
.idea
stages:
- build
- deploy
variables:
GIT_DEPTH: "1"
build:
stage: build
tags:
- linux
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/
upload_to_minio:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- aws s3 --endpoint=https://minio.mycard.moe:9000 sync --delete dist/ s3://nanahira/koishi-plugin/cache-memcached
only:
- master
deploy_npm:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- apt update;apt -y install coreutils
- echo $NPMRC | base64 --decode > ~/.npmrc
- npm publish . || true
only:
- master
/install-npm.sh
.git*
/output
/dest
/config.yaml
.idea
.dockerignore
Dockerfile
/src
.eslintrc.js
.prettierrc
webpack.config.js
tsconfig.json
\ No newline at end of file
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
The MIT License (MIT)
Copyright (c) 2021 Nanahira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Koishi Plugin Cache Memcached
# koishi-plugin-cache-memcached
Memcached cache support for Koishi.js
\ No newline at end of file
Koishi.js 的 Memcached 缓存支持。
## 安装
### npm
```bash
npm install koishi-plugin-cache-memcached
```
### 直接安装
在 https://cdn02.moecube.com:444/nanahira/koishi-plugin/cache-memcached/index.js 下载即可。
## 配置
* `endpoint` Memcached 地址。格式为 `[user:pass@]server1[:11211],[user:pass@]server2[:11211],...` ,参照 [MemJS 文档](https://memjs.netlify.app/)
* `memcachedClientOptions` MemJS 的额外可选配置。
* `prefix` 存储键前缀。默认 `koishi:`
#!/bin/bash
npm install --save \
source-map-support
npm install --save-dev \
@types/node \
typescript \
'@typescript-eslint/eslint-plugin@^4.28.2' \
'@typescript-eslint/parser@^4.28.2 '\
'eslint@^7.30.0' \
'eslint-config-prettier@^8.3.0' \
'eslint-plugin-prettier@^3.4.0' \
prettier \
raw-loader \
ts-loader \
webpack \
webpack-cli \
koishi@^4.0.0-alpha.9 \
koishi-utils-schemagen
This diff is collapsed.
{
"name": "koishi-plugin-cache-memcached",
"version": "1.0.0",
"description": "Memcached cache support for Koishi.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint --fix .",
"build": "webpack"
},
"repository": {
"type": "git",
"url": "https://code.mycard.moe/3rdeye/koishi-plugin-cache-memcached.git"
},
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"keywords": [
"bot",
"qqbot",
"cqhttp",
"coolq",
"chatbot",
"koishi",
"service:cache",
"memcached",
"memjs"
],
"bugs": {
"url": "https://code.mycard.moe/3rdeye/koishi-plugin-cache-memcached/issues"
},
"homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-cache-memcached",
"dependencies": {
"@types/memjs": "^1.2.3",
"source-map-support": "^0.5.20"
},
"peerDependencies": {
"koishi": "^4.0.0-alpha.9"
},
"devDependencies": {
"@types/moment": "^2.13.0",
"@types/node": "^16.11.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"encoded-buffer": "^0.2.6",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"koishi": "^4.0.0-alpha.9",
"koishi-utils-schemagen": "^1.1.4",
"memjs": "^1.3.0",
"moment": "^2.29.1",
"prettier": "^2.4.1",
"raw-loader": "^4.0.2",
"ts-loader": "^9.2.6",
"typescript": "^4.4.4",
"webpack": "^5.58.2",
"webpack-cli": "^4.9.1"
}
}
import 'source-map-support/register';
import { DefineSchema } from 'koishi-utils-schemagen';
import { ClientOptions } from 'memjs';
export class MemcachedCachePluginConfig {
@DefineSchema({ desc: 'Memcached 服务器地址', default: 'localhost:11211' })
endpoint: string;
@DefineSchema({
desc: 'Memcached 服务器选项',
type: 'object',
allowUnknown: true,
})
memcachedClientOptions: ClientOptions;
@DefineSchema({ desc: '存储键前缀', default: 'koishi:' })
prefix: string;
}
export type MemcachedCachePluginConfigLike = Partial<MemcachedCachePluginConfig>;
import 'source-map-support/register';
import { Context } from 'koishi';
import { MemcachedCache, MemcachedCachePlugin } from './plugin';
import { MemcachedCachePluginConfigLike } from './config';
export * from './config';
export * from './plugin';
export const name = 'memcached';
const plugin = new MemcachedCachePlugin();
export const schema = plugin.schema;
export function apply(
ctx: Context,
config: Partial<MemcachedCachePluginConfigLike>,
) {
ctx.plugin(plugin, config);
}
import 'source-map-support/register';
import { Context, Schema, Cache, Logger } from 'koishi';
import {
MemcachedCachePluginConfig,
MemcachedCachePluginConfigLike,
} from './config';
import { schemaFromClass, schemaTransform } from 'koishi-utils-schemagen';
import { Client } from 'memjs';
import { Moment } from 'moment';
import moment from 'moment';
import * as EncodeBuffer from 'encoded-buffer';
export class MemcachedCache extends Cache {
private logger = new Logger('memcached');
private mem: Client;
constructor(ctx: Context, private config: MemcachedCachePluginConfig) {
super(ctx);
this.mem = Client.create(config.endpoint, config.memcachedClientOptions);
}
private replaceKey(key: string) {
return key.replace(/\s+/g, '_');
}
private momentToBuffer(time: Moment) {
const buf = Buffer.allocUnsafe(4);
buf.writeUInt32LE(time.unix());
return buf;
}
private bufferToMoment(buf: Buffer) {
return moment.unix(buf.readUInt32LE(0));
}
private getAgeOfTable(table: keyof Cache.Tables) {
return this.table(table)?.maxAge || 63072000;
}
private getVersioningKey(table: keyof Cache.Tables) {
return this.replaceKey(`${this.config.prefix}${table}:currentVersion`);
}
private async resetVersion(table: keyof Cache.Tables) {
const versioningKey = this.getVersioningKey(table);
const value = this.momentToBuffer(moment());
try {
const result = await this.mem.set(versioningKey, value, {
expires: this.getAgeOfTable(table),
});
if (!result) {
this.logger.warn(`Set versioning key of table ${table} failed.`);
}
} catch (e) {
this.logger.error(
`Set versioning key of table ${table} errored: ${e.toString()}`,
);
}
return this.bufferToMoment(value).unix().toString(36);
}
private async getVersion(table: keyof Cache.Tables) {
const versioningKey = this.getVersioningKey(table);
const { value } = await this.mem.get(versioningKey);
if (!value) {
return this.resetVersion(table);
}
return this.bufferToMoment(value).unix().toString(36);
}
private async getKey(table: keyof Cache.Tables, key: string) {
const actualKey = this.replaceKey(key);
return `${this.config.prefix}${table}:${await this.getVersion(
table,
)}:${actualKey}`;
}
private async encode(data: any): Promise<Buffer> {
return EncodeBuffer.encodeAsync(data);
}
private async decode<T = any>(record: Buffer): Promise<T> {
const [value] = await EncodeBuffer.decodeAsync(record);
return value;
}
async get(table: keyof Cache.Tables, key: string) {
const actualKey = await this.getKey(table, key);
try {
const { value } = await this.mem.get(actualKey);
if (value == null) {
return;
}
const decodedValue = await this.decode(value);
this.logger.info(`${actualKey} => ${JSON.stringify(decodedValue)}`);
return decodedValue;
} catch (e) {
this.logger.error(`Get key of ${actualKey} errored: ${e.toString()}`);
return;
}
}
async set(
table: keyof Cache.Tables,
key: string,
value: any,
maxAge?: number,
) {
if (!this.table(table)) {
return;
}
const actualKey = await this.getKey(table, key);
const age = maxAge || this.getAgeOfTable(table);
try {
let result: boolean;
if (value == null) {
result = await this.mem.delete(actualKey);
} else {
result = await this.mem.set(actualKey, await this.encode(value), {
expires: age,
});
}
if (!result) {
this.logger.error(`Set key of ${actualKey} failed.`);
}
} catch (e) {
this.logger.error(`Set key of ${actualKey} errored: ${e.toString()}`);
}
}
async clear(table: keyof Cache.Tables) {
await this.resetVersion(table);
return;
}
}
export class MemcachedCachePlugin {
private config: MemcachedCachePluginConfig;
private ctx: Context;
name = 'memcached-main';
schema: Schema<MemcachedCachePluginConfigLike> = schemaFromClass(
MemcachedCachePluginConfig,
);
apply(ctx: Context, config: MemcachedCachePluginConfigLike) {
this.ctx = ctx;
this.config = schemaTransform(MemcachedCachePluginConfig, config);
ctx.cache = new MemcachedCache(ctx, this.config);
}
}
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2021",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts"
]
}
const path = require("path");
module.exports = {
entry: "./src/index.ts",
mode: "production",
target: "node",
devtool: "source-map",
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
},
{ test: /\.mustache$/, use: "raw-loader" },
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
output: {
filename: "index.js",
library: {
type: "commonjs",
},
path: path.resolve(__dirname, "dist"),
},
externals: {
'koishi': 'koishi',
}
};
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