mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-09 03:26:38 +08:00
redis refactor
This commit is contained in:
parent
b956656ed9
commit
34c0d0eaa1
@ -1,40 +1,28 @@
|
|||||||
// redisRestClient.ts
|
// redisRestClient.ts
|
||||||
import fetch from 'node-fetch'; // or any other fetch-compatible API
|
import { Redis } from "@upstash/redis";
|
||||||
|
|
||||||
const redisRestUrl = process.env.UPSTASH_REDIS_URL;
|
const redis = new Redis({
|
||||||
const redisRestToken = process.env.UPSTASH_REDIS_REST_TOKEN;
|
url: process.env.UPSTASH_REDIS_URL,
|
||||||
|
token: process.env.UPSTASH_REDIS_REST_TOKEN,
|
||||||
|
});
|
||||||
|
|
||||||
if (!redisRestUrl || !redisRestToken) {
|
if (!redis.url || !redis.token) {
|
||||||
console.error('The Upstash Redis URL and token must be provided.');
|
console.error('The Upstash Redis URL and token must be provided.');
|
||||||
// Handle the lack of Redis client here, e.g., by disabling certain features
|
// Handle the lack of Redis client here, e.g., by disabling certain features
|
||||||
}
|
}
|
||||||
|
|
||||||
const headers = {
|
|
||||||
Authorization: `Bearer ${redisRestToken}`,
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
};
|
|
||||||
|
|
||||||
export const incrementSignInCount = async (email: string | undefined, dateKey: string) => {
|
export const incrementSignInCount = async (email: string | undefined, dateKey: string) => {
|
||||||
if (!email) {
|
if (!email) {
|
||||||
console.error('Email is undefined, cannot increment sign-in count.');
|
console.error('Email is undefined, cannot increment sign-in count.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(`${redisRestUrl}/hincrby`, {
|
try {
|
||||||
method: 'POST',
|
await redis.hincrby(`signin_count:${email}`, dateKey, 1);
|
||||||
headers: headers,
|
} catch (error) {
|
||||||
body: JSON.stringify({
|
console.error('Failed to increment sign-in count in Redis via Upstash', error);
|
||||||
key: `signin_count:${email}`,
|
}
|
||||||
field: dateKey,
|
};
|
||||||
increment: 1
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
console.error('Failed to increment sign-in count in Redis via REST API');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
export const incrementSessionRefreshCount = async (email: string | undefined, dateKey: string) => {
|
export const incrementSessionRefreshCount = async (email: string | undefined, dateKey: string) => {
|
||||||
if (!email) {
|
if (!email) {
|
||||||
@ -42,18 +30,10 @@ export const incrementSessionRefreshCount = async (email: string | undefined, da
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(`${redisRestUrl}/hincrby`, {
|
try {
|
||||||
method: 'POST',
|
await redis.hincrby(`session_refreshes:${email}`, dateKey, 1);
|
||||||
headers: headers,
|
} catch (error) {
|
||||||
body: JSON.stringify({
|
console.error('Failed to increment session refresh count in Redis via Upstash', error);
|
||||||
key: `session_refreshes:${email}`,
|
|
||||||
field: dateKey,
|
|
||||||
increment: 1
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
console.error('Failed to increment session refresh count in Redis via REST API');
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -68,31 +48,10 @@ export const incrementTokenCounts = async (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const incrementCompletionTokensResponse = await fetch(`${redisRestUrl}/hincrby`, {
|
try {
|
||||||
method: 'POST',
|
await redis.hincrby(`tokens:${email}`, `${dateKey}:completion_tokens`, completionTokens);
|
||||||
headers: headers,
|
await redis.hincrby(`tokens:${email}`, `${dateKey}:prompt_tokens`, promptTokens);
|
||||||
body: JSON.stringify({
|
} catch (error) {
|
||||||
key: `tokens:${email}`,
|
console.error('Failed to increment token counts in Redis via Upstash', error);
|
||||||
field: `${dateKey}:completion_tokens`,
|
|
||||||
increment: completionTokens
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const incrementPromptTokensResponse = await fetch(`${redisRestUrl}/hincrby`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: headers,
|
|
||||||
body: JSON.stringify({
|
|
||||||
key: `tokens:${email}`,
|
|
||||||
field: `${dateKey}:prompt_tokens`,
|
|
||||||
increment: promptTokens
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!incrementCompletionTokensResponse.ok) {
|
|
||||||
console.error('Failed to increment completion token count in Redis via REST API');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!incrementPromptTokensResponse.ok) {
|
|
||||||
console.error('Failed to increment prompt token count in Redis via REST API');
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -40,7 +40,9 @@
|
|||||||
"sass": "^1.59.2",
|
"sass": "^1.59.2",
|
||||||
"spark-md5": "^3.0.2",
|
"spark-md5": "^3.0.2",
|
||||||
"use-debounce": "^9.0.4",
|
"use-debounce": "^9.0.4",
|
||||||
"zustand": "^4.3.8"
|
"zustand": "^4.3.8",
|
||||||
|
"@upstash/redis": "^1.25.1"
|
||||||
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@tauri-apps/cli": "^1.4.0",
|
"@tauri-apps/cli": "^1.4.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user