lruInitHook function

HiHook lruInitHook({
  1. String uid = 'lru:init',
  2. List<String> events = const ['write', 'put'],
  3. String metaKey = 'meta',
  4. String lastAccessedKey = 'last_accessed',
  5. String accessCountKey = 'access_count',
  6. int nowProvider()?,
  7. HiPhase phase = HiPhase.post,
  8. int priority = 0,
})

Creates an LRU initialization hook for new entries.

Sets initial LRU metadata on write operations. Should run in post phase after successful writes.

Parameters

  • uid: Unique hook identifier, defaults to 'lru:init'
  • events: Events to listen for, defaults to 'write', 'put'
  • metaKey: Key in ctx.data for metadata
  • lastAccessedKey: Key in meta for last access timestamp
  • accessCountKey: Key in meta for access count
  • nowProvider: Function to get current time

Implementation

HiHook<dynamic, dynamic> lruInitHook({
  String uid = 'lru:init',
  List<String> events = const ['write', 'put'],
  String metaKey = 'meta',
  String lastAccessedKey = 'last_accessed',
  String accessCountKey = 'access_count',
  int Function()? nowProvider,
  HiPhase phase = HiPhase.post,
  int priority = 0,
}) {
  return HiHook<dynamic, dynamic>(
    uid: uid,
    events: events,
    phase: phase,
    priority: priority,
    handler: (payload, ctx) {
      final context = ctx as HiContext;
      final meta = Map<String, dynamic>.from(
        context.dataTracked[metaKey] as Map<String, dynamic>? ?? {},
      );

      final now = nowProvider?.call() ?? DateTime.now().millisecondsSinceEpoch;
      meta[lastAccessedKey] = now;
      meta[accessCountKey] = 1;

      context.dataTracked[metaKey] = meta;
      return const HiContinue();
    },
  );
}