Skip to content

Instantly share code, notes, and snippets.

@mikemix
Last active August 13, 2022 18:23
Show Gist options
  • Save mikemix/957f069044b06a36d11ea3c25f82f842 to your computer and use it in GitHub Desktop.
Save mikemix/957f069044b06a36d11ea3c25f82f842 to your computer and use it in GitHub Desktop.

Revisions

  1. mikemix revised this gist Aug 13, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions lru-example.php
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,13 @@
    /** @var LRUCacheInterface<User> $cache */
    $cache = new LRUCache(50);

    foreach ($tasks->findAll() as $task) {
    foreach ($taskRepository->findAll() as $task) {
    // get the user from cache
    // phpStorm will correctly resolve the $user as the User class
    $user = $cache->get($task->getUserId());

    if (null === $user) {
    $user = $users->findById($task->getUserId());
    $user = $userRepository->findById($task->getUserId());
    $cache->add($user->getId(), $user);
    }

  2. mikemix revised this gist Aug 13, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lru-example.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    foreach ($tasks->findAll() as $task) {
    // get the user from cache
    // phpStorm will correctly resolve the $user as the User class
    $user = $cache->get($task->getUserId()) ??
    $user = $cache->get($task->getUserId());

    if (null === $user) {
    $user = $users->findById($task->getUserId());
  3. mikemix renamed this gist Aug 13, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. mikemix created this gist Aug 13, 2022.
    17 changes: 17 additions & 0 deletions lru-example
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <?php

    /** @var LRUCacheInterface<User> $cache */
    $cache = new LRUCache(50);

    foreach ($tasks->findAll() as $task) {
    // get the user from cache
    // phpStorm will correctly resolve the $user as the User class
    $user = $cache->get($task->getUserId()) ??

    if (null === $user) {
    $user = $users->findById($task->getUserId());
    $cache->add($user->getId(), $user);
    }

    $task->execute($user);
    }