TypeScript में कैशिंग: Redis, Memcached और अंतर्निहित समाधान (उन्नत स्तर)

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें

उन्नत कैशिंग क्यों TypeScript अनुप्रयोगों के लिए आवश्यक है

\n\n

आधुनिक उच्च-लोड सिस्टम में, कैशिंग एक विकल्प नहीं रह गया है - यह आर्किटेक्चर का एक अनिवार्य तत्व है। Node.js के साथ काम करने वाले TypeScript डेवलपर्स के लिए, सही कैशिंग रणनीति चुनने से API प्रतिक्रिया समय 200 ms से घटाकर 2 ms किया जा सकता है और डेटाबेस लोड 80-90% तक कम किया जा सकता है। इस लेख में, हम उन्नत स्तर पर कैशिंग के तीन दृष्टिकोणों पर विचार करेंगे: Redis, Memcached और TypeScript में अंतर्निहित समाधान।

\n\n

1. TypeScript में अंतर्निहित कैशिंग: सरल Map से LRU-कैश तक

\n\n

अंतर्निहित समाधान मोनोलिथिक अनुप्रयोगों या माइक्रोसर्विसेज के लिए आदर्श हैं जहाँ वितरित कैश की आवश्यकता नहीं होती है। सबसे सरल विकल्प Map का उपयोग है, लेकिन यह प्रोडक्शन के लिए उपयुक्त नहीं है क्योंकि इसमें TTL और मेमोरी नियंत्रण का अभाव है।

\n\n

TTL के साथ LRU-कैश का कार्यान्वयन

\n\n

स्वचालित सफाई के साथ LRU (Least Recently Used) कैश के उन्नत कार्यान्वयन पर विचार करें:

\n\n
interface CacheEntry<T> {\n  value: T;\n  expiresAt: number;\n}\n\nclass LRUCache<T> {\n  private cache: Map<string, CacheEntry<T>>;\n  private readonly maxSize: number;\n  private readonly ttlMs: number;\n\n  constructor(maxSize: number = 100, ttlMs: number = 60000) {\n    this.cache = new Map();\n    this.maxSize = maxSize;\n    this.ttlMs = ttlMs;\n  }\n\n  get(key: string): T | undefined {\n    const entry = this.cache.get(key);\n    if (!entry) return undefined;\n\n    if (Date.now() > entry.expiresAt) {\n      this.cache.delete(key);\n      return undefined;\n    }\n\n    // कुंजी को अंत में ले जाएँ (सबसे ताज़ा)\n    this.cache.delete(key);\n    this.cache.set(key, entry);\n    return entry.value;\n  }\n\n  set(key: string, value: T): void {\n    if (this.cache.has(key)) {\n      this.cache.delete(key);\n    } else if (this.cache.size >= this.maxSize) {\n      // सबसे पुराना (पहला) तत्व हटाएँ\n      const oldestKey = this.cache.keys().next().value;\n      this.cache.delete(oldestKey);\n    }\n\n    this.cache.set(key, {\n      value,\n      expiresAt: Date.now() + this.ttlMs,\n    });\n  }\n\n  clear(): void {\n    this.cache.clear();\n  }\n}\n\n// उपयोग\nconst userCache = new LRUCache<User>(500, 300000); // 500 तत्व, 5 मिनट TTL
\n\n

उन्नत पैटर्न: कैशिंग डेकोरेटर

\n\n

कोड की सफाई के लिए TypeScript डेकोरेटर का उपयोग करें:

\n\n
function Cached(ttlMs: number = 60000) {\n  const cache = new LRUCache<any>(100, ttlMs);\n\n  return function (\n    target: any,\n    propertyKey: string,\n    descriptor: PropertyDescriptor\n  ) {\n    const originalMethod = descriptor.value;\n\n    descriptor.value = async function (...args: any[]) {\n      const key = `${propertyKey}_${JSON.stringify(args)}`;\n      const cached = cache.get(key);\n      if (cached !== undefined) return cached;\n\n      const result = await originalMethod.apply(this, args);\n      cache.set(key, result);\n      return result;\n    };\n\n    return descriptor;\n  };\n}\n\nclass UserService {\n  @Cached(120000) // 2 मिनट\n  async getUser(id: string): Promise<User> {\n    // DB से महँगा क्वेरी\n    return db.findUser(id);\n  }\n}
\n\n

2. Redis: उन्नत डेटा संरचनाओं के साथ वितरित कैशिंग

\n\n

Redis केवल key-value स्टोरेज नहीं है। जटिल डेटा संरचनाओं (Hash, List, Set, Sorted Set) के लिए इसका समर्थन उन्नत कैशिंग रणनीतियों को लागू करने की संभावनाएँ खोलता है।

\n\n

टैग द्वारा अमान्यकरण के साथ कैशिंग

\n\n

समस्या: एक इकाई को अपडेट करते समय, कई कैश को अमान्य करने की आवश्यकता होती है। समाधान - टैग:

\n\n
import Redis from 'ioredis';\n\nclass TaggedCache {\n  private redis: Redis;\n  private readonly keyPrefix: string;\n\n  constructor(redisUrl: string, prefix: string = 'cache') {\n    this.redis = new Redis(redisUrl);\n    this.keyPrefix = prefix;\n  }\n\n  async get<T>(key: string,                

ब्लॉग

पुस्तक अनुशंसाएँ