createLRUEvictHook function
Creates a hook that updates access count on put and evicts if needed.
Runs during metaUpdatePriorEntry. Sets access count and evicts LRU entry if cache exceeds max.
Implementation
PVCacheHook createLRUEvictHook({required int max, int priority = 0}) {
return PVCacheHook(
eventString: 'lru_evict',
eventFlow: EventFlow.metaUpdatePriorEntry,
priority: priority,
actionTypes: [ActionType.put],
hookFunction: (ctx) async {
// Get the global counter
final counterData = await ctx.meta.get(_LRU_COUNTER_KEY);
int counter = counterData?['counter'] ?? 0;
// Increment counter
counter++;
// Set access count for this entry
ctx.runtimeMeta['_lru_count'] = counter;
// Store the new global counter
await ctx.meta.put(_LRU_COUNTER_KEY, {'counter': counter});
// Check cache size and evict if needed
await _evictIfNeeded(ctx, max);
},
);
}