cache 放入同一个store中

This commit is contained in:
cnwhy
2023-12-15 08:48:34 +08:00
parent d20b2228f0
commit f945b440f1
3 changed files with 112 additions and 27 deletions
+106 -23
View File
@@ -1,7 +1,10 @@
import { getIdb } from "./db";
import type * as Gost from "./types";
import { getGost } from "../uitls/server";
const localCache = "localCache";
const savedServer = "savedServer";
// TODO: cache 放入不同一个store中
export class GostCommit<T = any> {
private storeName: string;
constructor(storeName: string) {
@@ -15,7 +18,7 @@ export class GostCommit<T = any> {
};
getList = async () => {
const idb = await this._getIdb();
return idb.getAllFromIndex(this.storeName,'_key_',this.key);
return idb.getAllFromIndex(this.storeName, "_key_", this.key);
};
get = async (name: string) => {
const idb = await this._getIdb();
@@ -34,7 +37,9 @@ export class GostCommit<T = any> {
const idb = await this._getIdb();
const t = idb.transaction(this.storeName, "readwrite");
const os = t.objectStore(this.storeName);
const old = await os.index('[name+_key_]').get(IDBKeyRange.only([name, this.key]));
const old = await os
.index("[name+_key_]")
.get(IDBKeyRange.only([name, this.key]));
await os.put(obj, old.id);
return t.done;
};
@@ -42,44 +47,122 @@ export class GostCommit<T = any> {
const idb = await this._getIdb();
const t = idb.transaction(this.storeName, "readwrite");
const os = t.objectStore(this.storeName);
const old = await os.index('[name+_key_]').get(IDBKeyRange.only([name, this.key]));
const old = await os
.index("[name+_key_]")
.get(IDBKeyRange.only([name, this.key]));
await os.delete(old.id);
return t.done
return t.done;
};
}
export const admissions = new GostCommit<Gost.AdmissionConfig>("admissions");
export const authers = new GostCommit<Gost.AdmissionConfig>("authers");
export const bypasses = new GostCommit<Gost.BypassConfig>("bypasses");
export const chains = new GostCommit<Gost.ChainConfig>("chains");
export const climiters = new GostCommit<Gost.LimiterConfig>("climiters");
export const limiters = new GostCommit<Gost.LimiterConfig>("limiters");
export const rlimiters = new GostCommit<Gost.LimiterConfig>("rlimiters");
export const hops = new GostCommit<Gost.HopConfig>("hops");
export const hosts = new GostCommit<Gost.HostsConfig>("hosts");
export const ingresses = new GostCommit<Gost.IngressConfig>("ingresses");
export const resolvers = new GostCommit<Gost.ResolverConfig>("resolvers");
export const services = new GostCommit<Gost.ServiceConfig>("services");
// cache 放入同一个store中
export class GostCommit1<T = any> {
private dsName = localCache;
private type: string;
constructor(type: string) {
this.type = type;
}
private get key() {
return getGost()?.addr;
}
private _getIdb = () => {
return getIdb(
`${this.dsName}|++id,_key_,_type_,[_type_+_key_],[name+_type_+_key_]`
);
};
private _getTransaction = async () => {
const idb = await this._getIdb();
const transaction = idb.transaction(this.dsName, "readwrite");
const store = transaction.objectStore(this.dsName);
return {
transaction,
store,
};
};
getList = async () => {
const idb = await this._getIdb();
return idb.getAllFromIndex(
this.dsName,
"[_type_+_key_]",
IDBKeyRange.only([this.type, this.key])
);
};
get = async (name: string) => {
const idb = await this._getIdb();
// return idb.get(this.name);
return idb.getFromIndex(
this.dsName,
"[name+_type_+_key_]",
IDBKeyRange.only([name, this.type, this.key])
);
};
add = async (obj: any) => {
const idb = await this._getIdb();
return idb.add(this.dsName, {
...obj,
_key_: this.key,
_type_: this.type,
});
};
put = async (name: string, obj: any) => {
const { transaction, store } = await this._getTransaction();
const old = await store
.index("[name+_type_+_key_]")
.get(IDBKeyRange.only([name, this.type, this.key]));
await store.put(obj, old.id);
return transaction.done;
};
delete = async (name: string) => {
const { transaction, store } = await this._getTransaction();
const old = await store
.index("[name+_type_+_key_]")
.get(IDBKeyRange.only([name, this.type, this.key]));
await store.delete(old.id);
return transaction.done;
};
}
export const admissions = new GostCommit1<Gost.AdmissionConfig>("admissions");
export const authers = new GostCommit1<Gost.AdmissionConfig>("authers");
export const bypasses = new GostCommit1<Gost.BypassConfig>("bypasses");
export const chains = new GostCommit1<Gost.ChainConfig>("chains");
export const climiters = new GostCommit1<Gost.LimiterConfig>("climiters");
export const limiters = new GostCommit1<Gost.LimiterConfig>("limiters");
export const rlimiters = new GostCommit1<Gost.LimiterConfig>("rlimiters");
export const hops = new GostCommit1<Gost.HopConfig>("hops");
export const hosts = new GostCommit1<Gost.HostsConfig>("hosts");
export const ingresses = new GostCommit1<Gost.IngressConfig>("ingresses");
export const resolvers = new GostCommit1<Gost.ResolverConfig>("resolvers");
export const services = new GostCommit1<Gost.ServiceConfig>("services");
export class ServerComm {
private static _storeName = "savedServer";
private static _getIdb() {
return getIdb(`${this._storeName}|addr`);
return getIdb({ [localCache]: `addr`, [savedServer]: "_key_" });
}
static async getAll() {
const idb = await this._getIdb();
return idb.getAll(this._storeName);
return idb.getAll(savedServer);
}
static async get(key: string) {
const idb = await this._getIdb();
return idb.get(this._storeName, key);
return idb.get(savedServer, key);
}
static async set(value: any) {
const idb = await this._getIdb();
return idb.put(this._storeName, value);
return idb.put(savedServer, value);
}
static async delete(key: string) {
static async deleteCache(key: string) {
const idb = await this._getIdb();
return idb.delete(this._storeName, key);
const t = idb.transaction([localCache], "readwrite");
const sCache = await t.objectStore(localCache);
const keys = await sCache.index("_key_").getAllKeys(IDBKeyRange.only(key));
await sCache.delete(keys);
}
static async delete(key: string, rmCache: boolean = false) {
const idb = await this._getIdb();
await idb.delete(savedServer, key);
if (rmCache) {
await this.deleteCache(key);
}
}
}
+5 -3
View File
@@ -14,7 +14,7 @@ type Module = {
const getAttr = (keyName: string) => ({
keyName,
api: (API as any)[keyName],
// localApi: (API as any)[keyName],
localApi: (LocalApi as any)[keyName],
rowKey: "name",
});
@@ -41,6 +41,7 @@ const modules: Module[] = [
name: "chain",
title: "转发链(Chain)",
subTitle: "转发链",
// localApi: (LocalApi as any)['chains'],
...getAttr("chains"),
},
{
@@ -89,9 +90,10 @@ const modules: Module[] = [
name: "service",
title: "服务(Service)",
subTitle: "服务",
// localApi: (API as any)['services'],
localApi: (LocalApi as any)['services'],
...getAttr("services"),
// localApi: (API as any)['services'],
// localApi: (LocalApi as any)['services'],
// localApi: (LocalApi as any)['services'],
},
];
+1 -1
View File
@@ -65,7 +65,7 @@ const PublicList: React.FC<Props> = (props) => {
},
{
title: "操作",
width: name === "service" ? 120 : 90,
width: localApi ? 120 : 90,
align: "right",
dataIndex: rowKey,
render: (value, record, index) => {