• Home
  • Tutorials
  • Shop
  • AI News
  • Services
  • JaggoRe AITry
Get Started
  • Login
  • Register
Neuraldemy
Cart / 0.00$

No products in the cart.

No Result
View All Result
Get Started
Neuraldemy
Get started
Home Web Development

Singleton pattern In JavaScript

Neuraldemy by Neuraldemy
March 2, 2026
in Web Development, Javascript
Reading Time: 2 mins read
A A

An in-memory cache system that guarantees a single global instance using the Singleton Pattern in JavaScript/TypeScript


// Singleton Pattern 

interface ICache<T> {
  set(key: string, value: T): void;
  get(key: string): T | undefined;
  has(key: string): boolean;
  clear(): void;
}

class InMemoryCache<T> implements ICache<T> {
  private static instance: InMemoryCache<any>;
  private cache: Map<string, T>;

  private constructor() {
    this.cache = new Map<string, T>();
    console.log("Cache initialized in memory");
  }

  public static getInstance<T>(): InMemoryCache<T> {
    if (!InMemoryCache.instance) {
      InMemoryCache.instance = new InMemoryCache<T>();
    }
    return InMemoryCache.instance;
  }

  public set(key: string, value: T): void {
    this.cache.set(key, value);
  }

  public get(key: string): T | undefined {
    return this.cache.get(key);
  }

  public has(key: string): boolean {
    return this.cache.has(key);
  }

  public clear(): void {
    this.cache.clear();
  }
}

// We get our instance.

const myCache1 = InMemoryCache.getInstance<string>();
myCache1.set("user_123", "Amritesh");

const myCache2 = InMemoryCache.getInstance<string>();

console.log("Value from cache 2:", myCache2.get("user_123")); // Should print "Amritesh"

console.log("Are both caches the exact same instance?", myCache1 === myCache2);
Code language: TypeScript (typescript)

Previous Post

Dynamic Array Implementation Modern C++

Next Post

The 5 Best Books for Learning Machine Learning Mathematics

Neuraldemy

Neuraldemy

This is Neuraldemy support. Subscribe to our YouTube channel for more.

Related Posts

Strategy Pattern In JavaScript

Factory Pattern In JavaScript

Inside OpenAI Military Deal and the Ousting of Anthropic

Nano Banana 2: Google’s New AI Image Generator Just Fixed the Speed vs. Quality Problem

The 5 Best Books for Learning Machine Learning Mathematics

Dynamic Array Implementation Modern C++

Next Post
Close-up of a smartphone displaying ChatGPT app held over AI textbook.

The 5 Best Books for Learning Machine Learning Mathematics

Nano Banana 2: Google’s New AI Image Generator Just Fixed the Speed vs. Quality Problem

Inside OpenAI Military Deal and the Ousting of Anthropic

  • Customer Support
  • Get Started
  • Ask Your ML Queries
  • Contact
  • Privacy Policy
  • Terms Of Use
Neuraldemy

© 2024 - A learning platform by Odist Magazine

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

*By registering into our website, you agree to the Terms & Conditions and Privacy Policy.
All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • Tutorials
  • Shop
  • AI News
  • Services
  • JaggoRe AI
  • Login
  • Sign Up
  • Cart
Order Details

© 2024 - A learning platform by Odist Magazine

This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
0