diff --git a/src/lib/api/menu.ts b/src/lib/api/menu.ts index e1404cc..32cdda1 100644 --- a/src/lib/api/menu.ts +++ b/src/lib/api/menu.ts @@ -1,6 +1,8 @@ // lib/api/menu-api.ts import { fetchGraphQL } from './graphql-client.js'; +import { cache } from '@lib/cache/manager.js'; +import { CACHE_TTL } from '@lib/cache/cache-ttl'; export interface MenuItem { id: string; @@ -40,26 +42,40 @@ export type MenuIdentifier = * Получить меню по идентификатору */ export async function fetchMenu(identifier: MenuIdentifier): Promise { - try { - // Определяем тип запроса на основе переданного идентификатора - if ('id' in identifier) { - return await fetchMenuById(identifier.id); - } - if ('location' in identifier) { - return await fetchMenuByLocation(identifier.location); - } - 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; - } + // Создаем ключ кеша на основе типа и значения + let cacheKey; + if ('id' in identifier) cacheKey = `menu:id:${identifier.id}`; + else if ('location' in identifier) cacheKey = `menu:location:${identifier.location}`; + else if ('slug' in identifier) cacheKey = `menu:slug:${identifier.slug}`; + else if ('name' in identifier) cacheKey = `menu:name:${identifier.name}`; + else return null; + + return await cache.wrap( + cacheKey, + async () => { + try { + // Определяем тип запроса на основе переданного идентификатора + if ('id' in identifier) { + return await fetchMenuById(identifier.id); + } + if ('location' in identifier) { + return await fetchMenuByLocation(identifier.location); + } + 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 { - const menu = await fetchMenu(identifier); - if (!menu || !menu.menuItems?.nodes?.length) { - return []; - } + const cacheKey = `menu:hierarchical:${JSON.stringify(identifier)}`; - 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 { - const menu = await fetchMenu(identifier); - if (!menu || !menu.menuItems?.nodes?.length) { - return []; - } + const cacheKey = `menu:flat:${JSON.stringify(identifier)}`; - 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 } + ); +} \ No newline at end of file diff --git a/src/pages/index.astro b/src/pages/index.astro index ae8924c..8d38a8d 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -38,7 +38,7 @@ export const prerender = false; pageInfo={pageInfo} type="latest" perLoad={11} - showCount={true} + showCount={false} />