add cached menus
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
// lib/api/menu-api.ts
|
// lib/api/menu-api.ts
|
||||||
|
|
||||||
import { fetchGraphQL } from './graphql-client.js';
|
import { fetchGraphQL } from './graphql-client.js';
|
||||||
|
import { cache } from '@lib/cache/manager.js';
|
||||||
|
import { CACHE_TTL } from '@lib/cache/cache-ttl';
|
||||||
|
|
||||||
export interface MenuItem {
|
export interface MenuItem {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -40,26 +42,40 @@ export type MenuIdentifier =
|
|||||||
* Получить меню по идентификатору
|
* Получить меню по идентификатору
|
||||||
*/
|
*/
|
||||||
export async function fetchMenu(identifier: MenuIdentifier): Promise<Menu | null> {
|
export async function fetchMenu(identifier: MenuIdentifier): Promise<Menu | null> {
|
||||||
try {
|
// Создаем ключ кеша на основе типа и значения
|
||||||
// Определяем тип запроса на основе переданного идентификатора
|
let cacheKey;
|
||||||
if ('id' in identifier) {
|
if ('id' in identifier) cacheKey = `menu:id:${identifier.id}`;
|
||||||
return await fetchMenuById(identifier.id);
|
else if ('location' in identifier) cacheKey = `menu:location:${identifier.location}`;
|
||||||
}
|
else if ('slug' in identifier) cacheKey = `menu:slug:${identifier.slug}`;
|
||||||
if ('location' in identifier) {
|
else if ('name' in identifier) cacheKey = `menu:name:${identifier.name}`;
|
||||||
return await fetchMenuByLocation(identifier.location);
|
else return null;
|
||||||
}
|
|
||||||
if ('slug' in identifier) {
|
return await cache.wrap(
|
||||||
return await fetchMenuBySlug(identifier.slug);
|
cacheKey,
|
||||||
}
|
async () => {
|
||||||
if ('name' in identifier) {
|
try {
|
||||||
return await fetchMenuByName(identifier.name);
|
// Определяем тип запроса на основе переданного идентификатора
|
||||||
}
|
if ('id' in identifier) {
|
||||||
|
return await fetchMenuById(identifier.id);
|
||||||
return null;
|
}
|
||||||
} catch (error) {
|
if ('location' in identifier) {
|
||||||
console.error('Error fetching menu:', error);
|
return await fetchMenuByLocation(identifier.location);
|
||||||
return null;
|
}
|
||||||
}
|
if ('slug' in identifier) {
|
||||||
|
return await fetchMenuBySlug(identifier.slug);
|
||||||
|
}
|
||||||
|
if ('name' in identifier) {
|
||||||
|
return await fetchMenuByName(identifier.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching menu:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ ttl: CACHE_TTL.MENU } // Используем константу из cache-ttl
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -294,22 +310,38 @@ export function buildMenuHierarchy(menuItems: MenuItem[]): MenuItem[] {
|
|||||||
* Получить меню в виде иерархической структуры
|
* Получить меню в виде иерархической структуры
|
||||||
*/
|
*/
|
||||||
export async function getHierarchicalMenu(identifier: MenuIdentifier): Promise<MenuItem[]> {
|
export async function getHierarchicalMenu(identifier: MenuIdentifier): Promise<MenuItem[]> {
|
||||||
const menu = await fetchMenu(identifier);
|
const cacheKey = `menu:hierarchical:${JSON.stringify(identifier)}`;
|
||||||
if (!menu || !menu.menuItems?.nodes?.length) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return buildMenuHierarchy(menu.menuItems.nodes);
|
return await cache.wrap(
|
||||||
|
cacheKey,
|
||||||
|
async () => {
|
||||||
|
const menu = await fetchMenu(identifier);
|
||||||
|
if (!menu || !menu.menuItems?.nodes?.length) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return buildMenuHierarchy(menu.menuItems.nodes);
|
||||||
|
},
|
||||||
|
{ ttl: CACHE_TTL.MENU }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Получить меню в виде плоского списка
|
* Получить меню в виде плоского списка
|
||||||
*/
|
*/
|
||||||
export async function getFlatMenu(identifier: MenuIdentifier): Promise<MenuItem[]> {
|
export async function getFlatMenu(identifier: MenuIdentifier): Promise<MenuItem[]> {
|
||||||
const menu = await fetchMenu(identifier);
|
const cacheKey = `menu:flat:${JSON.stringify(identifier)}`;
|
||||||
if (!menu || !menu.menuItems?.nodes?.length) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return menu.menuItems.nodes.sort((a, b) => a.order - b.order);
|
return await cache.wrap(
|
||||||
}
|
cacheKey,
|
||||||
|
async () => {
|
||||||
|
const menu = await fetchMenu(identifier);
|
||||||
|
if (!menu || !menu.menuItems?.nodes?.length) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return menu.menuItems.nodes.sort((a, b) => a.order - b.order);
|
||||||
|
},
|
||||||
|
{ ttl: CACHE_TTL.MENU }
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -38,7 +38,7 @@ export const prerender = false;
|
|||||||
pageInfo={pageInfo}
|
pageInfo={pageInfo}
|
||||||
type="latest"
|
type="latest"
|
||||||
perLoad={11}
|
perLoad={11}
|
||||||
showCount={true}
|
showCount={false}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user