caching_package 0.0.1 copy "caching_package: ^0.0.1" to clipboard
caching_package: ^0.0.1 copied to clipboard

A lightweight and efficient Flutter package for caching key-value data with optional expiration time support. Built on SharedPreferences, it provides type-safe data persistence with automatic cache in [...]

Caching Package #

A lightweight and efficient Flutter package for caching key-value data with optional expiration time support. Built on SharedPreferences, it provides type-safe data persistence with automatic cache invalidation.

pub package Build Status License: MIT

Features #

  • ๐Ÿš€ Simple and intuitive key-value data caching
  • โฐ Flexible expiration time for cached items
  • ๐Ÿ”’ Type-safe data retrieval with generics support
  • ๐ŸŽฏ Automatic cache invalidation
  • ๐Ÿ’พ Persistent storage using SharedPreferences
  • ๐Ÿ”„ Asynchronous API with Future support
  • ๐Ÿ“ฑ Cross-platform support (iOS, Android, Web, Desktop)
  • ๐Ÿงช Well-tested and production-ready

Demo #

Saving Data to Cache #

Saving to Cache

Retrieving Data from Cache #

Retrieving from Cache

Getting Started #

Installation #

  1. Add this to your package's pubspec.yaml file:
dependencies:
  caching_package: ^0.0.1
  1. Install the package:
flutter pub get
  1. Import the package in your Dart code:
import 'package:caching_package/caching_package.dart';

Usage #

Basic Usage #

// Store data in cache
await CacheManager.set('user_preferences', {
  'theme': 'dark',
  'notifications': true
});

// Retrieve data with type safety
Map<String, dynamic>? preferences = await CacheManager.get<Map<String, dynamic>>('user_preferences');

// Store data with expiration
await CacheManager.set('auth_token', 'xyz123',
    expireAfter: Duration(hours: 24));

// Clear specific data
await CacheManager.remove('auth_token');

// Clear all cached data
await CacheManager.clear();

Advanced Usage #

Caching Complex Objects

class User {
  final String name;
  final int age;
  
  User(this.name, this.age);
  
  Map<String, dynamic> toJson() => {
    'name': name,
    'age': age
  };
  
  factory User.fromJson(Map<String, dynamic> json) => User(
    json['name'] as String,
    json['age'] as int
  );
}

// Store complex object
final user = User('John Doe', 30);
await CacheManager.set('current_user', user.toJson());

// Retrieve and convert back to object
final userData = await CacheManager.get<Map<String, dynamic>>('current_user');
final cachedUser = userData != null ? User.fromJson(userData) : null;

Error Handling

try {
  await CacheManager.set('important_data', 'sensitive_info');
} catch (e) {
  print('Failed to cache data: $e');
}

try {
  final data = await CacheManager.get<String>('important_data');
  // Handle data
} catch (e) {
  print('Failed to retrieve cached data: $e');
}

Working with Lists

// Cache a list of items
List<String> todos = ['Buy milk', 'Walk dog', 'Do laundry'];
await CacheManager.set('todo_list', todos);

// Retrieve the list
List<dynamic>? cachedTodos = await CacheManager.get<List>('todo_list');
List<String> todoList = cachedTodos?.cast<String>() ?? [];

API Reference #

CacheManager #

Methods

static Future<void> set(String key, dynamic value, {Duration? expireAfter})
  • Stores data in the cache with an optional expiration duration
  • Parameters:
    • key: Unique identifier for the cached data
    • value: Data to be cached (must be JSON serializable)
    • expireAfter: Optional duration after which the data expires
static Future<T?> get<T>(String key)
  • Retrieves typed data from the cache
  • Parameters:
    • key: Unique identifier for the cached data
    • T: Type parameter for type-safe data retrieval
  • Returns: The cached value of type T, or null if not found or expired
static Future<void> remove(String key)
  • Removes specific data from the cache
  • Parameters:
    • key: Unique identifier for the cached data
static Future<void> clear()
  • Clears all cached data

Best Practices #

  1. Type Safety: Always specify the correct type when retrieving data:

    final count = await CacheManager.get<int>('counter');
    
  2. Error Handling: Implement proper error handling for cache operations

  3. Expiration Times: Set appropriate expiration times for sensitive data

  4. Complex Objects: Implement toJson and fromJson for complex objects

Contributing #

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create your feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

Issues and Feedback #

Please file issues, bugs, or feature requests in our issue tracker.

License #

This project is licensed under the MIT License - see the LICENSE file for details.

0
likes
140
points
27
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A lightweight and efficient Flutter package for caching key-value data with optional expiration time support. Built on SharedPreferences, it provides type-safe data persistence with automatic cache invalidation.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on caching_package