fastcache 1.0.0 copy "fastcache: ^1.0.0" to clipboard
fastcache: ^1.0.0 copied to clipboard

A fast and efficient caching library for Dart applications.

example/fastcache_example.dart

import 'package:fastcache/fastcache.dart';

void main() async {
  // ------------------------------
  // 1. Basic Cache Operations
  // ------------------------------
  print('=== Basic Cache Operations ===');
  final cache = FastCache<String, int>(3); // Create cache with capacity of 3
  
  // Add items using set method
  await cache.set('a', 1);
  await cache.set('b', 2);
  await cache.set('c', 3);
  
  // Get items using get method
  print('a: ${await cache.get('a')}'); // Output: a: 1
  print('b: ${await cache.get('b')}'); // Output: b: 2
  print('c: ${await cache.get('c')}'); // Output: c: 3
  
  // Use subscript operator
  cache['d'] = 4; // Exceeds capacity, 'a' (oldest) will be evicted
  print('d: ${cache['d']}'); // Output: d: 4
  print('a evicted (oldest): ${await cache.get('a')}'); // Output: a evicted (oldest): null
  print('b still exists: ${await cache.get('b')}'); // Output: b still exists: 2
  
  // ------------------------------
  // 2. LRU Policy Demonstration
  // ------------------------------
  print('\n=== LRU Policy Demonstration ===');
  // Current order: b (most recent), c, d (oldest after b,c were accessed)
  // Access b to make it most recent
  await cache.get('b'); 
  await cache.get('c');
  await cache.get('d');
  
  await cache.set('e', 5); // Exceeds capacity, b is oldest now
  print('b evicted (oldest): ${await cache.get('b')}'); // Output: b evicted (oldest): null
  print('e added: ${await cache.get('e')}'); // Output: e added: 5
  print('c still exists: ${await cache.get('c')}'); // Output: c still exists: 3
  
  // ------------------------------
  // 3. TTL Expiration Feature
  // ------------------------------
  print('\n=== TTL Expiration Feature ===');
  final ttlCache = FastCache<String, String>();
  
  // Set item with TTL (expires after 2 seconds)
  await ttlCache.set(
    'temp', 
    'temporary value',
    period: Duration(seconds: 2),
    onExpire: (key, value) {
      print('$key expired with value: $value');
    }
  );
  
  print('temp: ${await ttlCache.get('temp')}'); // Output: temp: temporary value
  print('Waiting 3 seconds...');
  await Future.delayed(Duration(seconds: 3));
  print('After expiration: ${await ttlCache.get('temp')}'); // Output: After expiration: null
  
  // ------------------------------
  // 4. Auto-update Feature (Update Cycle)
  // ------------------------------
  print('\n=== Auto-update Feature ===');
  int counter = 0;
  final updateCache = FastCache<String, int>();
  
  // Set item with auto-update every 1 second
  await updateCache.set(
    'auto-update',
    counter,
    updateCycle: Duration(seconds: 1),
    onUpdate: (key, oldValue) {
      counter++;
      print('Auto-updating $key: $oldValue -> $counter');
      return counter;
    }
  );
  
  // First get triggers initial update since value is marked as stale
  print('After initial get: ${await updateCache.get('auto-update')}'); // Output: After initial get: 1
  print('Waiting 1.5 seconds for auto-update...');
  await Future.delayed(Duration(milliseconds: 1500));
  print('After auto-update cycle: ${await updateCache.get('auto-update')}'); // Output: After auto-update cycle: 2
  
  // ------------------------------
  // 5. Callback Functions
  // ------------------------------
  print('\n=== Callback Functions ===');
  final callbackCache = FastCache<String, String>(2);
  
  // Set default callbacks
  callbackCache.onDefaultExpire = (key, value) {
    print('Expire callback: $key -> $value');
  };
  
  callbackCache.onDefaultEvict = (key, value) {
    print('Evict callback: $key -> $value');
  };
  
  await callbackCache.set('x', 'value1');
  await callbackCache.set('y', 'value2');
  await callbackCache.set('z', 'value3'); // x (oldest) gets evicted
  
  // Now set item with TTL to demonstrate expire callback
  await callbackCache.set('temp', 'temp-value', period: Duration(seconds: 1));
  print('Waiting 1.5 seconds for expiration...');
  await Future.delayed(Duration(milliseconds: 1500));
  print('After temp expires: ${await callbackCache.get('temp')}');
  
  // ------------------------------
  // 6. Synchronous Operations
  // ------------------------------
  print('\n=== Synchronous Operations ===');
  final syncCache = FastCache<int, String>();
  
  syncCache.setSync(1, 'one');
  syncCache.setSync(2, 'two');
  
  print('Sync get 1: ${syncCache.getSync(1)}'); // Output: one
  print('Sync get 2: ${syncCache.getSync(2)}'); // Output: two
  
  syncCache.removeSync(1);
  print('After remove 1: ${syncCache.getSync(1)}'); // Output: null
  
  // ------------------------------
  // 7. Batch Operations
  // ------------------------------
  print('\n=== Batch Operations ===');
  final batchCache = FastCache<String, int>();
  
  await batchCache.set('a', 1);
  await batchCache.set('b', 2);
  await batchCache.set('c', 3);
  
  print('All keys: ${await batchCache.keys}'); // Output: (a, b, c)
  print('All values: ${await batchCache.values}'); // Output: (1, 2, 3)
  print('Cache size: ${await batchCache.length}'); // Output: 3
  
  await batchCache.clear();
  print('After clear size: ${await batchCache.length}'); // Output: 0
  
  print('\n=== Demo Complete ===');
}
0
likes
0
points
233
downloads

Publisher

unverified uploader

Weekly Downloads

A fast and efficient caching library for Dart applications.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

synchronized

More

Packages that depend on fastcache