Skip to content

API Caching

The apiCache middleware caches responses from read-only Telegram API methods to reduce redundant network calls.

Quick Start

typescript
import { Bot, apiCache } from 'vibegram';

const bot = new Bot('YOUR_BOT_TOKEN');

// Cache API responses for 5 minutes
bot.use(apiCache({ ttl: 300 }));

bot.command('info', async (ctx) => {
    const chat = await ctx.getChat();    // hits API
    const again = await ctx.getChat();   // returns cached
    await ctx.reply(`Chat: ${chat.title}`);
});

Cached Methods

Only idempotent, read-only API methods are cached:

MethodDescription
getChatChat information
getChatMemberMember info and status
getChatMemberCountMember count
getChatAdministratorsAdmin list
getFileFile download link
getMeBot information
getMyCommandsCommand list
getStickerSetSticker set data
getUserProfilePhotosUser photos

Write methods (sendMessage, editMessage, deleteMessage, etc.) are never cached.

Options

OptionTypeDefaultDescription
ttlnumber300Time-to-live in seconds
storeCacheStoreMemoryCacheStorage backend
keyGeneratorfunctionAutoCustom cache key function

Custom Store

Implement the CacheStore interface for Redis or other backends:

typescript
import { CacheStore } from 'vibegram';

class RedisCache implements CacheStore {
    async get(key: string) {
        const data = await redis.get(key);
        return data ? JSON.parse(data) : undefined;
    }
    async set(key: string, value: any, ttlMs: number) {
        await redis.set(key, JSON.stringify(value), 'PX', ttlMs);
    }
    async delete(key: string) { await redis.del(key); }
    async clear() { await redis.flushdb(); }
}

bot.use(apiCache({ ttl: 600, store: new RedisCache() }));

Standalone Cache

Use cached() to wrap any async function:

typescript
import { cached } from 'vibegram';

const fetchUser = cached(
    async (userId: number) => db.users.findById(userId),
    { ttl: 60 }
);

const user = await fetchUser(123); // cached for 60 seconds

Memory Management

The built-in MemoryCache enforces a hard cap (default: 10,000 entries) with LRU eviction — same strategy as the session store.

typescript
import { MemoryCache } from 'vibegram';

const store = new MemoryCache(5000); // max 5,000 entries
bot.use(apiCache({ store }));

Released under the ISC License.