in_memory_cache 1.0.0
A simple in-memory cache for Dart and Flutter applications.
InMemoryCache
Overview
InMemoryCache is a Dart class that provides a simple in-memory caching mechanism. It allows for the storage of key-value pairs with optional expiration times, facilitating efficient data retrieval and temporary storage solutions.
Features
- In-Memory Storage: Utilizes Dart's
HashMapfor highly efficient key-value storage. - Optional Expiration: Supports automatic removal of entries after a specified duration.
- Basic Cache Operations: Provides methods to insert, retrieve, check existence, remove, and clear cache entries.
- Iterable Access: Offers access to all keys and values stored in the cache.
Usage
Importing the Class
To use the InMemoryCache class, ensure you have the necessary import:
Creating an Instance
You can create an instance of InMemoryCache with or without an expiration duration.
With Expiration Duration:
final cache = InMemoryCache<String, String>.withExpiry(
expiryDuration: Duration(seconds: 5),
);
In this example, entries added to the cache will expire and be removed after 5 seconds.
Without Expiration Duration:
final cache = InMemoryCache<String, String>.persistent();
Here, entries will persist in the cache until explicitly removed.
Inserting Data
Add entries to the cache using the insert method:
cache.insert('username', 'JohnDoe');
If an expiration duration is set, the entry will be automatically removed after the specified time.
Retrieving Data
Access stored values using the get method:
String? username = cache.get('username');
if (username != null) {
print('Username: $username');
} else {
print('Key not found or expired');
}
This will print the username if it exists; otherwise, it indicates that the key is not found or has expired.
Checking for Key Existence
Verify if a key exists in the cache:
if (cache.containsKey('username')) {
print('Key exists in cache');
} else {
print('Key does not exist');
}
Removing Entries
Remove a specific entry from the cache:
cache.remove('username');
To clear all entries:
cache.clear();
Accessing All Keys and Values
Retrieve all keys and values stored in the cache:
print('Keys: ${cache.keys.toList()}');
print('Values: ${cache.values.toList()}');
Warning: Since HashMap does not maintain order, retrieving all values may return them in an unordered fashion.
Advantages of Using HashMap
The InMemoryCache class leverages Dart's HashMap due to its performance benefits:
- Fast Lookup: Provides an average time complexity of O(1) for insertions, deletions, and lookups.
- Efficient Memory Usage: Optimized for scenarios where quick access to data is essential.
- Key-Based Access: Allows for straightforward retrieval of data without the need for iteration, making it significantly faster than using a
Mapwhere iteration might be required. - Dynamic Growth: Efficiently manages varying amounts of data by dynamically adjusting its capacity.
- Unordered Data Storage: Since
HashMapdoes not guarantee any specific order of keys or values, retrieval order may be inconsistent.
Why HashMap is the Best Choice for Caching?
Using a HashMap is the best approach for caching because caching mechanisms primarily rely on accessing values through their keys rather than iterating through all entries. This ensures faster and more efficient lookups compared to using a Map, where iteration might be necessary. HashMap provides near-instant access to stored data, making it ideal for performance-critical applications.