Building a reliable caching system: Sharing of design and practical experience of Java caching mechanism
Introduction:
In most applications, data caching is an improvement A common approach to system performance. Caching reduces access to the underlying data source, significantly improving application response time. In Java, we can implement the caching mechanism in a variety of ways. This article will introduce some common caching design patterns and practical experiences, and provide specific code examples.
1. Cache design pattern:
import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; public class InMemoryCache<T> { private final Map<String, CacheEntry<T>> cache; private final long expirationTime; private static class CacheEntry<T> { private final T value; private final long createTime; CacheEntry(T value) { this.value = value; this.createTime = System.currentTimeMillis(); } boolean isExpired(long expirationTime) { return System.currentTimeMillis() - createTime > expirationTime; } } public InMemoryCache(long expirationTime) { this.cache = new HashMap<>(); this.expirationTime = expirationTime; } public void put(String key, T value) { cache.put(key, new CacheEntry<>(value)); } public T get(String key) { CacheEntry<T> entry = cache.get(key); if (entry != null && !entry.isExpired(expirationTime)) { return entry.value; } else { cache.remove(key); return null; } } public static void main(String[] args) { InMemoryCache<String> cache = new InMemoryCache<>(TimeUnit.MINUTES.toMillis(30)); cache.put("key1", "value1"); String value = cache.get("key1"); System.out.println(value); } }
import java.io.*; import java.util.HashMap; import java.util.Map; public class DiskCache<T> { private final Map<String, File> cache; public DiskCache() { this.cache = new HashMap<>(); } public void put(String key, T value) { try { File file = new File("cache/" + key + ".bin"); ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file)); outputStream.writeObject(value); outputStream.close(); cache.put(key, file); } catch (IOException e) { e.printStackTrace(); } } public T get(String key) { File file = cache.get(key); if (file != null && file.exists()) { try { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file)); T value = (T) inputStream.readObject(); inputStream.close(); return value; } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } cache.remove(key); return null; } public static void main(String[] args) { DiskCache<String> cache = new DiskCache<>(); cache.put("key1", "value1"); String value = cache.get("key1"); System.out.println(value); } }
2. Caching practical experience:
3. Conclusion:
By properly designing and using the caching mechanism, the performance and response speed of the application can be significantly improved. When building a reliable cache system, choose an appropriate cache strategy, perform cache cleaning and expiration regularly, and consider the consistency of distributed caches. This article provides specific code examples of memory- and disk-based caching design patterns, hoping to help readers build reliable caching systems.
References:
The above is the detailed content of Building a stable and reliable caching system: Sharing experience in the design and implementation of Java caching mechanism. For more information, please follow other related articles on the PHP Chinese website!