Commit 1c7b7b7e authored by nanahira's avatar nanahira

add prefix in keys

parent 47674147
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
"better-lock": "^2.0.3", "better-lock": "^2.0.3",
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
"encoded-buffer": "^0.2.6", "encoded-buffer": "^0.2.6",
"ioredis": "^5.2.2", "ioredis": "^5.2.3",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"lru-cache": "^7.13.1", "lru-cache": "^7.13.1",
"typed-reflector": "^1.0.11" "typed-reflector": "^1.0.11"
...@@ -2725,9 +2725,9 @@ ...@@ -2725,9 +2725,9 @@
"dev": true "dev": true
}, },
"node_modules/ioredis": { "node_modules/ioredis": {
"version": "5.2.2", "version": "5.2.3",
"resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.2.2.tgz", "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.2.3.tgz",
"integrity": "sha512-wryKc1ur8PcCmNwfcGkw5evouzpbDXxxkMkzPK8wl4xQfQf7lHe11Jotell5ikMVAtikXJEu/OJVaoV51BggRQ==", "integrity": "sha512-gQNcMF23/NpvjCaa1b5YycUyQJ9rBNH2xP94LWinNpodMWVUPP5Ai/xXANn/SM7gfIvI62B5CCvZxhg5pOgyMw==",
"dependencies": { "dependencies": {
"@ioredis/commands": "^1.1.1", "@ioredis/commands": "^1.1.1",
"cluster-key-slot": "^1.1.0", "cluster-key-slot": "^1.1.0",
...@@ -6934,9 +6934,9 @@ ...@@ -6934,9 +6934,9 @@
"dev": true "dev": true
}, },
"ioredis": { "ioredis": {
"version": "5.2.2", "version": "5.2.3",
"resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.2.2.tgz", "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.2.3.tgz",
"integrity": "sha512-wryKc1ur8PcCmNwfcGkw5evouzpbDXxxkMkzPK8wl4xQfQf7lHe11Jotell5ikMVAtikXJEu/OJVaoV51BggRQ==", "integrity": "sha512-gQNcMF23/NpvjCaa1b5YycUyQJ9rBNH2xP94LWinNpodMWVUPP5Ai/xXANn/SM7gfIvI62B5CCvZxhg5pOgyMw==",
"requires": { "requires": {
"@ioredis/commands": "^1.1.1", "@ioredis/commands": "^1.1.1",
"cluster-key-slot": "^1.1.0", "cluster-key-slot": "^1.1.0",
......
...@@ -126,20 +126,20 @@ export class Aragami { ...@@ -126,20 +126,20 @@ export class Aragami {
); );
} }
async clear(base: AnyClass | string) { async clear(base: AnyClass | string, prefix?: string) {
return this.driver.clear(this.getBaseKey(base)); return this.driver.clear(this.getBaseKey(base), prefix);
} }
async keys(base: AnyClass | string) { async keys(base: AnyClass | string, prefix?: string) {
return this.driver.keys(this.getBaseKey(base)); return this.driver.keys(this.getBaseKey(base), prefix);
} }
async values<T>(cl: ClassType<T>) { async values<T>(cl: ClassType<T>, prefix?: string) {
const buffers = await this.driver.values(this.getBaseKey(cl)); const buffers = await this.driver.values(this.getBaseKey(cl), prefix);
return buffers.map((buf) => this.decode(cl, buf)); return buffers.map((buf) => this.decode(cl, buf));
} }
async entries<T>(cl: ClassType<T>): Promise<[string, T][]> { async entries<T>(cl: ClassType<T>, prefix?: string): Promise<[string, T][]> {
const entries = await this.driver.entries(this.getBaseKey(cl)); const entries = await this.driver.entries(this.getBaseKey(cl));
return entries.map(([key, buf]) => [key, this.decode(cl, buf)]); return entries.map(([key, buf]) => [key, this.decode(cl, buf)]);
} }
......
...@@ -22,24 +22,24 @@ export class BaseDriver { ...@@ -22,24 +22,24 @@ export class BaseDriver {
return false; return false;
} }
async keys(baseKey: string): Promise<string[]> { async keys(baseKey: string, prefix?: string): Promise<string[]> {
return []; return [];
} }
async values(baseKey: string): Promise<Buffer[]> { async values(baseKey: string, prefix?: string): Promise<Buffer[]> {
const keys = await this.keys(baseKey); const keys = await this.keys(baseKey, prefix);
return Promise.all(keys.map((key) => this.get(baseKey, key))); return Promise.all(keys.map((key) => this.get(baseKey, key)));
} }
async entries(baseKey: string): Promise<[string, Buffer][]> { async entries(baseKey: string, prefix?: string): Promise<[string, Buffer][]> {
const keys = await this.keys(baseKey); const keys = await this.keys(baseKey, prefix);
return Promise.all( return Promise.all(
keys.map(async (key) => [key, await this.get(baseKey, key)]), keys.map(async (key) => [key, await this.get(baseKey, key)]),
); );
} }
async clear(baseKey: string) { async clear(baseKey: string, prefix?: string) {
const keys = await this.keys(baseKey); const keys = await this.keys(baseKey, prefix);
await Promise.all(keys.map((key) => this.del(baseKey, key))); await Promise.all(keys.map((key) => this.del(baseKey, key)));
} }
......
...@@ -47,14 +47,27 @@ export class MemoryDriver extends BaseDriver { ...@@ -47,14 +47,27 @@ export class MemoryDriver extends BaseDriver {
return cache.delete(key); return cache.delete(key);
} }
override async keys(baseKey: string): Promise<string[]> { override async keys(baseKey: string, prefix?: string): Promise<string[]> {
const cache = this.getCacheInstance(baseKey); const cache = this.getCacheInstance(baseKey);
return Array.from(cache.keys()); let keys = Array.from(cache.keys());
if (prefix) {
keys = keys.filter((key) => key.startsWith(prefix));
}
return keys;
} }
override async clear(baseKey: string): Promise<void> { override async clear(baseKey: string, prefix?: string): Promise<void> {
const cache = this.getCacheInstance(baseKey); const cache = this.getCacheInstance(baseKey);
cache.clear(); if (prefix) {
const keys = Array.from(cache.keys());
for (const key of keys) {
if (key.startsWith(prefix)) {
cache.delete(key);
}
}
} else {
cache.clear();
}
} }
override lock<R>(keys: string[], cb: () => Promise<R>): Promise<R> { override lock<R>(keys: string[], cb: () => Promise<R>): Promise<R> {
......
...@@ -38,17 +38,17 @@ export class RedisDriver extends BaseDriver { ...@@ -38,17 +38,17 @@ export class RedisDriver extends BaseDriver {
return !!this.redis.del(this.usingKey(baseKey, key)); return !!this.redis.del(this.usingKey(baseKey, key));
} }
private originalKeys(baseKey: string) { private originalKeys(baseKey: string, prefix = '') {
return this.redis.keys(this.usingKey(baseKey, '*')); return this.redis.keys(this.usingKey(baseKey, `${prefix}*`));
} }
override async keys(baseKey: string): Promise<string[]> { override async keys(baseKey: string, prefix?: string): Promise<string[]> {
const keys = await this.originalKeys(baseKey); const keys = await this.originalKeys(baseKey, prefix ?? '');
return keys.map((key) => key.slice(baseKey.length + 1)); return keys.map((key) => key.slice(baseKey.length + 1));
} }
override async clear(baseKey: string): Promise<void> { override async clear(baseKey: string, prefix?: string): Promise<void> {
const keys = await this.originalKeys(baseKey); const keys = await this.originalKeys(baseKey, prefix);
if (!keys.length) { if (!keys.length) {
return; return;
} }
......
...@@ -7,7 +7,7 @@ describe('Aragami.', () => { ...@@ -7,7 +7,7 @@ describe('Aragami.', () => {
beforeEach(() => { beforeEach(() => {
aragami = new Aragami({ aragami = new Aragami({
redis: { uri: 'redis://localhost:6379' }, redis: process.env.REDIS ? { uri: 'redis://localhost:6379' } : undefined,
}); });
}); });
...@@ -45,6 +45,7 @@ describe('Aragami.', () => { ...@@ -45,6 +45,7 @@ describe('Aragami.', () => {
expect(userGet).toEqual({ name: 'John', age: 30 }); expect(userGet).toEqual({ name: 'John', age: 30 });
expect(userGet).toBeInstanceOf(User); expect(userGet).toBeInstanceOf(User);
await expect(aragami.keys(User)).resolves.toEqual(['n.John']); await expect(aragami.keys(User)).resolves.toEqual(['n.John']);
await expect(aragami.keys(User, 'n.J')).resolves.toEqual(['n.John']);
await expect(aragami.values(User)).resolves.toEqual([ await expect(aragami.values(User)).resolves.toEqual([
{ name: 'John', age: 30 }, { name: 'John', age: 30 },
]); ]);
......
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