PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.38k stars 451 forks source link

How do I delete a specific item? #851

Closed MathiasReker closed 2 years ago

MathiasReker commented 2 years ago

Configuration (optional)

My question

I have used this great library for a while, thanks!

I have one issue: I cannot delete a specific cache item. Can you help me understand what I am doing wrong?

I have something like this for saving the item - this is working great, and I can receive the cache. Also, I can clear all caches.

    public function save(string $html): void
    {
        try {
            $key = $this->getKey(); // for the specific case, $key this is: **01765bbaf27f4d1467cb0878208f6ccbc9463770**
            $item = $this->cache->getItem($key);

            if (!$item->isHit()) {
                $item->set($html)->expiresAfter(self::TTL);
                $this->cache->save($item);
            }
        } catch (PhpfastcacheInvalidArgumentException $e) {
            LogService::error($e->getMessage(), $e->getTrace());
        }
    }

However, I can't figure out how to delete a specific item. I tried something like this:

    public function deleteItem($key) // $key = **01765bbaf27f4d1467cb0878208f6ccbc9463770**
    {
        $this->cache->deleteItem($key);
    }

Doing this will clear the whole cache.

What am I missing? 🤔

github-actions[bot] commented 2 years ago

Hello curious contributor ! Since it seems to be your first contribution, make sure that you've been:

Geolim4 commented 2 years ago

Hello,

Please read the PSR-6 documentation. 🤷🏻

Thanks.

Geolim4 commented 2 years ago

Also if you want a simplified way to deal with cache you can use our PSR-16 adapter .

MathiasReker commented 2 years ago

This should do it, but it deletes every item in the cache: $this->cache->deleteItem($key); 🤔

Geolim4 commented 2 years ago

I don't know what to say, maybe a wrong implementation from your project... Phpfastcache is heavily tested and such a big bug would immediately thrown the CI down :/

MathiasReker commented 2 years ago

You are right, something is wrong with my implementation. I just can't figure out what is wrong. I will do some more research. Thanks,