Commit caa1a891 authored by nanahira's avatar nanahira

catchup for del method

parent 6f414b26
Pipeline #6337 passed with stages
in 37 seconds
{ {
"name": "koishi-plugin-cache-memcached", "name": "koishi-plugin-cache-memcached",
"version": "1.0.0", "version": "1.0.1",
"description": "Memcached cache support for Koishi.js", "description": "Memcached cache support for Koishi.js",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
......
import 'source-map-support/register'; import 'source-map-support/register';
import { Context, Schema, Cache, Logger } from 'koishi'; import { Context, Schema, Cache, Logger, isNullable } from 'koishi';
import { import {
MemcachedCachePluginConfig, MemcachedCachePluginConfig,
MemcachedCachePluginConfigLike, MemcachedCachePluginConfigLike,
...@@ -92,7 +92,7 @@ export class MemcachedCache extends Cache { ...@@ -92,7 +92,7 @@ export class MemcachedCache extends Cache {
return; return;
} }
const decodedValue = await this.decode(value); const decodedValue = await this.decode(value);
this.logger.info(`${actualKey} => ${JSON.stringify(decodedValue)}`); // this.logger.info(`${actualKey} => ${JSON.stringify(decodedValue)}`);
return decodedValue; return decodedValue;
} catch (e) { } catch (e) {
this.logger.error(`Get key of ${actualKey} errored: ${e.toString()}`); this.logger.error(`Get key of ${actualKey} errored: ${e.toString()}`);
...@@ -109,25 +109,35 @@ export class MemcachedCache extends Cache { ...@@ -109,25 +109,35 @@ export class MemcachedCache extends Cache {
if (!this.table(table)) { if (!this.table(table)) {
return; return;
} }
if (isNullable(value)) {
return this.del(table, key);
}
const actualKey = await this.getKey(table, key); const actualKey = await this.getKey(table, key);
const age = maxAge || this.getAgeOfTable(table); const age = maxAge || this.getAgeOfTable(table);
try { try {
let result: boolean; const result = await this.mem.set(actualKey, await this.encode(value), {
if (value == null) {
result = await this.mem.delete(actualKey);
} else {
result = await this.mem.set(actualKey, await this.encode(value), {
expires: age, expires: age,
}); });
}
if (!result) { if (!result) {
this.logger.error(`Set key of ${actualKey} failed.`); this.logger.warn(`Set key of ${actualKey} failed.`);
} }
} catch (e) { } catch (e) {
this.logger.error(`Set key of ${actualKey} errored: ${e.toString()}`); this.logger.error(`Set key of ${actualKey} errored: ${e.toString()}`);
} }
} }
async del(table: keyof Cache.Tables, key: string) {
const actualKey = await this.getKey(table, key);
try {
const result = await this.mem.delete(actualKey);
if (!result) {
this.logger.warn(`Delete key ${actualKey} failed.`);
}
} catch (e) {
this.logger.error(`Delete key ${actualKey} errored: ${e.toString()}`);
}
}
async clear(table: keyof Cache.Tables) { async clear(table: keyof Cache.Tables) {
await this.resetVersion(table); await this.resetVersion(table);
return; return;
......
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