gehrisandro / tailwind-merge-php

Automatically resolves Tailwind CSS class conflicts in PHP
MIT License
112 stars 4 forks source link

Optimize Caching Logic #13

Open maltekuhr opened 4 weeks ago

maltekuhr commented 4 weeks ago

Hi there! 👋

I'm excited to contribute to the tailwind-merge-php package. I've found an opportunity to optimize the caching logic, which should improve performance slightly. It cuts the number of cache calls in half.

What I discovered:

What I did to fix it:

  1. Simplified the caching logic by directly attempting to retrieve the value from the cache.
  2. If the retrieved value is not a string (including null for non-existent keys), we simply continue with the existing logic.
  3. This change eliminates the need for a separate has() check, potentially improving performance.

Here's the exact change:

Before

if ($this->cache->has($key)) {
    $cachedValue = $this->cache->get($key);

    if (is_string($cachedValue)) {
        return $cachedValue;
    }
}

After

$cachedValue = $this->cache->get($key);
if (is_string($cachedValue)) {
    return $cachedValue;
}

I hope this helps improve the package! Let me know if you'd like me to explain anything further or make any changes.

Thanks for considering my contribution! 😊