WikiGalaxy

Personalize

Types of Caches: In-Memory, Distributed, and Local

In-Memory Cache

In-memory caches store data in the system's RAM, providing fast data retrieval. They are ideal for temporary data storage and quick access, reducing the load on databases.

Distributed Cache

Distributed caches spread across multiple nodes or servers, providing scalability and fault tolerance. They are used in large-scale systems to ensure data availability and consistency.

Local Cache

Local caches are specific to an application instance, storing data locally. They are useful for reducing network latency and improving application performance.

In-Memory Cache with Java

Java HashMap

Using Java's HashMap as an in-memory cache for storing temporary data, ensuring fast retrieval and updates.


import java.util.HashMap;

public class InMemoryCache {
    private HashMap<String, String> cache = new HashMap<>();

    public void put(String key, String value) {
        cache.put(key, value);
    }

    public String get(String key) {
        return cache.get(key);
    }

    public static void main(String[] args) {
        InMemoryCache cache = new InMemoryCache();
        cache.put("user1", "John Doe");
        System.out.println("Cached User: " + cache.get("user1"));
    }
}
    

Efficiency

The HashMap provides O(1) time complexity for put and get operations, making it highly efficient for caching purposes.

Use Case

Ideal for applications requiring fast data retrieval without persistent storage.

Distributed Cache with Redis

Redis Overview

Redis is an open-source, in-memory data structure store, used as a distributed cache to improve data retrieval speed and scalability.


// Pseudo-code example for Redis usage
import redis.clients.jedis.Jedis;

public class RedisCache {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost");
        jedis.set("user2", "Jane Doe");
        System.out.println("Cached User: " + jedis.get("user2"));
        jedis.close();
    }
}
    

Scalability

Redis supports data replication, partitioning, and persistence, making it suitable for distributed caching in large-scale systems.

Use Case

Used in applications requiring high availability and low latency access to data across distributed systems.

Local Cache with Ehcache

Ehcache Overview

Ehcache is a widely used Java-based cache for general-purpose caching, often used for local caching within an application instance.


// Pseudo-code example for Ehcache usage
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EhcacheExample {
    public static void main(String[] args) {
        CacheManager cacheManager = CacheManager.newInstance();
        Cache cache = cacheManager.getCache("myCache");
        cache.put(new Element("user3", "Alice"));
        System.out.println("Cached User: " + cache.get("user3").getObjectValue());
        cacheManager.shutdown();
    }
}
    

Performance

Ehcache provides fast access to frequently used data, reducing the need for repetitive database queries.

Use Case

Ideal for applications where data consistency is less critical, and performance is prioritized.

In-Memory Cache with Caffeine

Caffeine Overview

Caffeine is a high-performance Java caching library, providing an in-memory cache with automatic eviction based on size or time.


import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

public class CaffeineCache {
    private Cache<String, String> cache = Caffeine.newBuilder()
            .maximumSize(100)
            .build();

    public void put(String key, String value) {
        cache.put(key, value);
    }

    public String get(String key) {
        return cache.getIfPresent(key);
    }

    public static void main(String[] args) {
        CaffeineCache cache = new CaffeineCache();
        cache.put("user4", "Bob");
        System.out.println("Cached User: " + cache.get("user4"));
    }
}
    

Automatic Eviction

Caffeine supports automatic eviction policies, ensuring efficient memory usage by removing stale data.

Use Case

Suitable for applications requiring high throughput and low latency with frequent cache updates.

Distributed Cache with Hazelcast

Hazelcast Overview

Hazelcast is an in-memory data grid that provides distributed caching, enabling data sharing across a cluster of nodes.


// Pseudo-code example for Hazelcast usage
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

public class HazelcastCache {
    public static void main(String[] args) {
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        IMap<String, String> map = hz.getMap("my-distributed-map");
        map.put("user5", "Charlie");
        System.out.println("Cached User: " + map.get("user5"));
        hz.shutdown();
    }
}
    

Cluster Support

Hazelcast supports clustering, allowing data to be partitioned and replicated across nodes for fault tolerance and scalability.

Use Case

Ideal for distributed systems requiring data synchronization and consistency across multiple nodes.

Local Cache with Guava

Guava Overview

Guava is a set of core Java libraries, including a local caching library that provides simple and efficient caching mechanisms.


import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.concurrent.TimeUnit;

public class GuavaCache {
    private LoadingCache<String, String> cache = CacheBuilder.newBuilder()
            .expireAfterWrite(10, TimeUnit.MINUTES)
            .build(new CacheLoader<>() {
                public String load(String key) {
                    return "default";
                }
            });

    public void put(String key, String value) {
        cache.put(key, value);
    }

    public String get(String key) {
        return cache.getUnchecked(key);
    }

    public static void main(String[] args) {
        GuavaCache cache = new GuavaCache();
        cache.put("user6", "Dave");
        System.out.println("Cached User: " + cache.get("user6"));
    }
}
    

Expiration Policy

Guava supports expiration policies, allowing cached data to be invalidated after a specified time period.

Use Case

Ideal for applications needing lightweight caching with automatic expiration of stale data.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025