Implement basic caching

This commit is contained in:
2023-04-23 14:23:20 +07:00
parent 89c5b2fa8b
commit 59554b9fd8
3 changed files with 181 additions and 81 deletions
+111 -13
View File
@@ -1,34 +1,58 @@
import { Configuration, AuthenticationApi, UsersApi, WorldsApi } from 'vrchat';
import * as VRChat from 'vrchat';
import Environment from './Environment';
import Logger from '../libs/Logger';
import App from './App';
const VRC_CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
interface VRChatAPIs {
AuthenticationApi: AuthenticationApi;
UsersApi: UsersApi;
WorldsApi: WorldsApi;
AuthenticationApi: VRChat.AuthenticationApi;
UsersApi: VRChat.UsersApi;
WorldsApi: VRChat.WorldsApi;
}
interface UserCache {
user: VRChat.User;
expires: Date;
}
interface WorldCache {
world: VRChat.World;
expires: Date;
}
interface Cache {
Users: { [key: string]: UserCache };
Worlds: { [key: string]: WorldCache };
}
class VRChatAPI {
public client: VRChatAPIs | null;
public configuration: Configuration | null;
public configuration: VRChat.Configuration | null;
private cache: Cache;
private useragent: string | null = null;
constructor() {
this.cache = {
Users: {},
Worlds: {}
};
this.client = null;
this.configuration = null;
}
public async init() {
this.configuration = new Configuration({
this.useragent = `Yumi/${App.version.replaceAll('/', '').replaceAll(' ', '-').replaceAll('--', '-')} ${
Environment.get().VRC_CONTACT_EMAIL
}`;
Logger.debug(`[VRChat] Using user agent ${this.useragent}`);
this.configuration = new VRChat.Configuration({
//username: Environment.get().VRC_USERNAME,
//;password: Environment.get().VRC_PASSWORD,
apiKey: Environment.get().VRC_API_KEY,
baseOptions: {
headers: {
'User-Agent': `Yumi/${App.version.replaceAll('/', '').replaceAll(' ', '-').replaceAll('--', '-')} ${
Environment.get().VRC_CONTACT_EMAIL
}`,
'User-Agent': this.useragent,
'Content-Type': 'application/json',
Cookie: Environment.get().VRC_COOKIE || ''
}
@@ -36,15 +60,89 @@ class VRChatAPI {
});
this.client = {
AuthenticationApi: new AuthenticationApi(this.configuration),
UsersApi: new UsersApi(this.configuration),
WorldsApi: new WorldsApi(this.configuration)
AuthenticationApi: new VRChat.AuthenticationApi(this.configuration),
UsersApi: new VRChat.UsersApi(this.configuration),
WorldsApi: new VRChat.WorldsApi(this.configuration)
};
Logger.info('Logging in to VRChat');
let res = await this.client.AuthenticationApi.getCurrentUser();
Logger.info('Logged in to VRChat as ' + res.data.displayName);
setInterval(() => {
for (let key in this.cache.Users) {
if (this.cache.Users[key].expires < new Date()) {
Logger.debug('[VRChat] [Cache] Removing user ' + key + ' from cache');
delete this.cache.Users[key];
}
}
for (let key in this.cache.Worlds) {
if (this.cache.Worlds[key].expires < new Date()) {
Logger.debug('[VRChat] [Cache] Removing world ' + key + ' from cache');
delete this.cache.Worlds[key];
}
}
}, VRC_CACHE_DURATION);
}
public async getCachedUserById(id: string) {
if (typeof this.cache.Users[id] !== 'undefined') {
Logger.debug('[VRChat] [Cache] Cache hit for user ' + id);
if (this.cache.Users[id].expires > new Date()) {
return this.cache.Users[id].user;
}
}
Logger.debug('[VRChat] [Cache] Cache miss for ' + id);
let user;
try {
user = await this.client!.UsersApi.getUser(id);
} catch (err: any) {
if (err.response?.status === 404) return null;
throw err;
}
if (!user) {
return null;
}
Logger.debug('[VRChat] [Cache] Caching user ' + id);
this.cache.Users[id] = {
user: user.data,
expires: new Date(Date.now() + VRC_CACHE_DURATION)
};
return user.data;
}
public async getCachedWorldById(id: string) {
if (typeof this.cache.Worlds[id] !== 'undefined') {
Logger.debug('[VRChat] [Cache] Cache hit for world ' + id);
if (this.cache.Worlds[id].expires > new Date()) {
return this.cache.Worlds[id].world;
}
}
Logger.debug('[VRChat] [Cache] Cache miss for ' + id);
let world;
try {
world = await this.client!.WorldsApi.getWorld(id);
} catch (err: any) {
if (err.response?.status === 404) return null;
throw err;
}
if (!world) {
return null;
}
Logger.debug('[VRChat] [Cache] Caching world ' + id);
this.cache.Worlds[id] = {
world: world.data,
expires: new Date(Date.now() + VRC_CACHE_DURATION)
};
return world.data;
}
public end(): void {}