lruInitHook function
HiHook
lruInitHook({})
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 metadatalastAccessedKey: Key in meta for last access timestampaccessCountKey: Key in meta for access countnowProvider: 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();
},
);
}