Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Manager.php #3

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open

Update Manager.php #3

wants to merge 1 commit into from

Conversation

zikezhang
Copy link
Contributor

implement invalidateCache()

implement `invalidateCache()`
@headio
Copy link
Owner

headio commented Mar 6, 2023

Hi,
Agreed, one can remove the old key explicitly.

I'd like to get your thoughts on the current implementation...

invalidateCache() increments the existing cache key, when a model is created or updated for example like so:

if ($this->cache->has($key)) {
    $this->cache->getAdapter()->setForever(
        $key,
        microtime(true),
    );
}

Thus the unique identifier changes and the data is written to the new key; the old key will never be requested again. And whilst this generates a lot of cache garbage, cache eviction is best handled by the underlying cache adapter by means of a cache eviction policy. In other words, "Redis" (or another) can be configured to handle the truncation of redundant keys based on some definition.

What are your thoughts on this technique vs deleting the cache key explicitly?

All the best,
Dominic

@zikezhang
Copy link
Contributor Author

$this->cache->delete($key);

when the cached has been deleted, the Memory(for Redis) or space(Other Adapters) will be released immediately.

if ($this->cache->has($key)) {
    $this->cache->getAdapter()->setForever(
        $key,
        microtime(true),
    );
}

Yes, the $key has been updated. But it's still assigned a new value. If the Adapter is Stream, the file would be there forever in the disk.

Another thing I want to know is how to use the CacheInvalidateable inside Headio\Phalcon\ServiceLayer\Model\Behavior.
the variable $collection there should be the model name.
and inside the cache manager. the function:

public function invalidateCache(array $collection): void

the $key is based on the model name(remove some string.). This means once the model is confirmed the $key is unique.

However, in the function appendCache() the $key is hashed by an array().

I am not sure if I have used it correctly. But I can't make get the updated data from the cache.

Thanks,
Jeremy

@zikezhang
Copy link
Contributor Author

Hi headio,

Thanks for your great work. I have figured out how to trigger the invalidateCache(). Everything works fine. :)

@headio
Copy link
Owner

headio commented Mar 8, 2023

Hi Jeremy,

Apologies for my late response. Nice to hear that you have it working.

I will put together a Gist or demo repo where I can show in detail how I have been using the library, e.g. cache invalidation for one entity and multiple related entities etc. Also Registering a "Service provider" for the service layer and how the entity and behavior declarations look. Also some info on how I have configured Redis for cache eviction—same strategy applies to Apcu and Memcached too.

I'm looking at your PR re. deleting the cache explicitly... give me a few days to test.

All the best,
Dominic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants