createLRUTrackAccessHook function

PVCacheHook createLRUTrackAccessHook({
  1. int priority = 100,
})

Creates a hook that updates access count on get.

Implementation

PVCacheHook createLRUTrackAccessHook({int priority = 100}) {
  return PVCacheHook(
    eventString: 'lru_track_access',
    eventFlow: EventFlow.metaRead,
    priority: priority,
    actionTypes: [ActionType.get],
    hookFunction: (ctx) async {
      // Get the global counter
      final counterData = await ctx.meta.get(_LRU_COUNTER_KEY);
      int counter = counterData?['counter'] ?? 0;

      // Increment counter
      counter++;

      // Update entry's access count
      ctx.runtimeMeta['_lru_count'] = counter;

      // Store the new global counter
      await ctx.meta.put(_LRU_COUNTER_KEY, {'counter': counter});
    },
  );
}