create/update actions and redis cahce store completed tested

This commit is contained in:
2025-06-21 23:44:52 +03:00
parent 06ca2b0835
commit 311736ce06
19 changed files with 1717 additions and 48 deletions

View File

@@ -0,0 +1,123 @@
"use server";
import { functionRetrieveUserSelection } from "@/fetchers/fecther";
import { AuthError } from "@/fetchers/types/context";
import { getCompleteFromRedis, setCompleteToRedis } from "@/fetchers/custom/context/complete/fetch";
import { safeRedisGet, safeJsonParse } from "@/utils/redisOperations";
import { REDIS_TIMEOUT } from "@/fetchers/base";
// Import or define the ClientRedisToken type
import type { ClientRedisToken } from "@/fetchers/types/context";
// Define the cache structure
type CacheData = {
[url: string]: {
[key: string]: any;
};
};
// Extend the ClientRedisToken type to include cache
type ExtendedClientRedisToken = ClientRedisToken & {
cache: CacheData;
};
/**
* Get cached form values from Redis for a specific URL
* @param url The URL key to retrieve cached form values for
* @returns The cached form values or null if not found
*/
const getCacheFromRedis = async (url: string): Promise<any> => {
try {
const decrpytUserSelection = await functionRetrieveUserSelection();
if (!decrpytUserSelection) throw new AuthError("No user selection found");
const redisKey = decrpytUserSelection?.redisKey;
if (!redisKey) throw new AuthError("No redis key found");
// Use safe Redis get operation with proper connection handling
const result = await safeRedisGet(`${redisKey}`, REDIS_TIMEOUT);
if (!result) return null;
// Use safe JSON parsing with proper default object type
const parsedResult = safeJsonParse(result, { cache: {} }) as { cache: CacheData };
// Return the cached data for the specific URL or null if not found
return parsedResult.cache && parsedResult.cache[url] ? parsedResult.cache[url] : null;
} catch (error) {
console.error("Error getting cache from Redis:", error);
return null;
}
};
/**
* Set cached form values in Redis for a specific URL
* @param url The URL key to store the form values under
* @param formValues The form values to cache
* @returns True if successful, false otherwise
*/
const setCacheToRedis = async (url: string, formValues: any): Promise<boolean> => {
try {
const decrpytUserSelection = await functionRetrieveUserSelection();
if (!decrpytUserSelection) throw new AuthError("No user selection found");
const redisKey = decrpytUserSelection?.redisKey;
if (!redisKey) throw new AuthError("No redis key found");
// Get the complete data from Redis
const completeData = await getCompleteFromRedis() as ExtendedClientRedisToken;
if (!completeData) throw new AuthError("No complete data found in Redis");
// Initialize cache object if it doesn't exist
if (!completeData.cache) {
completeData.cache = {} as CacheData;
}
// Update the cache with the new form values for the specific URL
completeData.cache[url] = formValues;
// Save the updated data back to Redis
await setCompleteToRedis(completeData);
return true;
} catch (error) {
console.error("Error setting cache to Redis:", error);
if (error instanceof AuthError) {
throw error;
} else {
throw new AuthError(error instanceof Error ? error.message : "Unknown error");
}
}
};
/**
* Clear cached form values in Redis for a specific URL
* @param url The URL key to clear cached form values for
* @returns True if successful, false otherwise
*/
const clearCacheFromRedis = async (url: string): Promise<boolean> => {
try {
const decrpytUserSelection = await functionRetrieveUserSelection();
if (!decrpytUserSelection) throw new AuthError("No user selection found");
const redisKey = decrpytUserSelection?.redisKey;
if (!redisKey) throw new AuthError("No redis key found");
// Get the complete data from Redis
const completeData = await getCompleteFromRedis() as ExtendedClientRedisToken;
if (!completeData) throw new AuthError("No complete data found in Redis");
// If cache exists and has the URL, delete it
if (completeData.cache && completeData.cache[url]) {
delete completeData.cache[url];
// Save the updated data back to Redis
await setCompleteToRedis(completeData as any);
}
return true;
} catch (error) {
console.error("Error clearing cache from Redis:", error);
return false;
}
};
export { getCacheFromRedis, setCacheToRedis, clearCacheFromRedis };