Skip to content

Commit

Permalink
Add post about LRUCache
Browse files Browse the repository at this point in the history
  • Loading branch information
freedomlewis committed Apr 30, 2024
1 parent bf30e77 commit e608b7f
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions _posts/2024-04-30-LRUCache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Unveiling the Android LRUCache - A Peek Under the Hood
date: 2024-04-30 12:02:00 +0800
categories: [Tech]
tags: [Android]
render_with_liquid: false
---

Ever wondered how Android keeps your frequently used data readily available while maintaining smooth performance? A significant player in this game is the LRUCache (Least Recently Used Cache). This powerful class efficiently manages data storage, prioritizing what you use most often. But LRUCache doesn't operate in isolation; it relies on a trusted companion called LinkedHashMap. In this blog, we'll dissect the inner workings of Android's LRUCache implementation, exploring how it leverages LinkedHashMap to achieve optimal performance.

## Understanding LRU Caching: The Balancing Act

Imagine you're constantly switching between apps on your phone. LRU Cache steps in to ensure a seamless experience. It prioritizes data you use frequently, storing it within easy reach. When new data arrives and the cache reaches its predefined size limit, LRUCache gracefully evicts the least recently used data, making space for the new information. This ensures you have fast access to the most relevant data, optimizing app performance and user experience.

## The Brains of the Operation: LRUCache's Internal Workings

At the core of LRUCache lies LinkedHashMap. It provides a robust foundation with a doubly linked list structure. Think of each data entry (key-value pair) as a train car linked to the one before and after it. This structure facilitates efficient operations like adding, removing, and accessing entries based on their usage order.

### Key Methods in Action:

- **put(key, value):** This method is the workhorse for adding new entries. It checks if the cache is full. If so, it triggers the eviction process (more on that later).
- **get(key):** This method retrieves a data entry based on the provided key. If found, it might also update the entry's position within the linked list, signifying recent use (like moving a train car to the front of the locomotive).
- **remove(key):** This method removes a specific entry from the cache, essentially unlinking it from the train.
- **sizeOf(key, value) (Optional):** This method allows for customization. You can define how the cache calculates the size of each entry. By default, it considers the object's memory footprint.

## Eviction Strategy: Making Way for the New

When the cache reaches its capacity and a new entry needs a spot, eviction becomes necessary. LRUCache employs a clear strategy: it removes the eldest entry (the caboose of our train analogy). This ensures the cache prioritizes the most recently used data, optimizing resource utilization.

## The Unsung Hero: How LinkedHashMap Supports LRUCache

While LRUCache takes the spotlight, LinkedHashMap silently contributes to the success of the operation. Here's how:

- **Ordered Structure:** LinkedHashMap maintains the doubly linked list, ensuring entries are ordered based on their access time.
- **Fast Access and Updates:** This structure allows for quick retrieval and update of entries based on their position in the list.
- **Efficient Removal:** When eviction is necessary, LinkedHashMap efficiently removes the eldest entry from the linked list.

## A Well-Oiled Machine: Collaboration for Efficiency

LRUCache leverages the strengths of LinkedHashMap to achieve efficient LRU caching. The combination provides a robust and scalable solution for managing data in Android applications.

## Beyond the Basics: Customizing the Cache for Specific Needs

The beauty of LRUCache lies in its flexibility. Here's how you can tailor it for your applications:

- **Override Methods:** Techniques like overriding the `entryRemoved` method allow you to define custom behavior when an entry is evicted (e.g., releasing resources associated with the evicted data).
- **Size Calculation:** The `sizeOf` method lets you customize how the cache calculates the size of each entry.

## Unveiling the Secrets: Conclusion

By understanding the intricate collaboration between LRUCache and LinkedHashMap, you gain valuable insight into how Android manages data efficiently. This knowledge empowers you to optimize your own applications and leverage caching techniques effectively. So next time you experience the seamless performance of Android, remember the silent heroes – LRUCache and LinkedHashMap – working tirelessly in the background!

0 comments on commit e608b7f

Please sign in to comment.