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

(dev/core#174) CRM_Utils_Cache_Redis::flush() - Respect prefixes #12330

Merged
merged 1 commit into from
Jun 19, 2018

Conversation

totten
Copy link
Member

@totten totten commented Jun 18, 2018

Overview

When flushing caches for an instance of CRM_Utils_Cache_Redis, you should
only delete the items with the matching prefix.

For reference, consider this example:

$js_strings = CRM_Utils_Cache::create(array(
  'name' => 'js_strings',
  'type' => array('*memory*', 'SqlGroup', 'ArrayCache'),
));

$community_messages = CRM_Utils_Cache::create(array(
  'name' => 'community_messages',
  'type' => array('*memory*', 'SqlGroup', 'ArrayCache'),
));

$js_strings->flush();

If the system is generally configured for Redis, this will give us two instances of CRM_Utils_Cache_Redis -- these instances are stored on the same server but use different prefixes.

Before

Flushing js_strings has the side-effect of flushing everything else in Redis, such as community_messages.

After

Flushing js_strings only flushes js_strings.

Comments

The example above focuses on two relatively obscure caches (js_strings and community_messages) because they actually exist today. In the future, as part of dev/core#174, it will become possible to store sessions in Redis. And then it will be more important -- e.g. if a user creates a custom-field, you do want to flush one set of data (e.g. the contact-fields cache), but you don't want to flush another (e.g. the user sessions).

The current commit fixes Redis. Although it's outside the scope of this
commit, here's an assessment of other drivers:

  • APCcache looks fine -- it already consults the prefix.
  • ArrayCache looks fine -- data is already scoped within the object.
  • NoCache looks fine -- it's a null-op for everything.
  • SqlGroup is loosely right. There's a similar-but-different quirk, but a separate PR will address that.
  • Memcache and Memcached look buggy. I don't have these systems locally, so I haven't tested or patched.

When flushing caches for an instance of `CRM_Utils_Cache_Redis`, you should
only delete the items with the matching prefix.

For example, we have two cache services for `js_strings` and
`community_messages`.  These caches always use the same backend with
different naming/prefixes. The backend depends on the system configuration...
might be Redis, Memcache, or SqlGroup. Let's suppose the configuration is `Redis`.

To consider the before/after, it helps to have these two snippets for reference:

```
1: Civi::cache('js_strings')->flush();
2: Civi::cache('community_messages')->flush();
```

Before
------

Flushing `js_strings` has the side-effect of flushing everything
else in Redis, such as `community_messages`.

After
-----

Flushing `js_strings` only flushes `js_strings`.

Comments
--------

The example above focuses on two relatively obscure caches.  In the future,
as part of dev/core#174, it will become possible to store sessions in Redis.
And then it will be more important -- e.g.  if a user creates a custom-field,
you do want to flush one set of data (e.g. the contact-fields cache), but you
don't want to flush another (e.g. the user sessions).

The current commit fixes Redis.  Although it's outside the scope of this
commit, here's an assessment of other drivers:

* `APCcache`, `ArrayCache`, `NoCache` should be fine already.
* `SqlGroup` is sort of right, sort of not. Another patch will address that.
* `Memcache` and `Memcached` look like they have the same bug. I don't
  have these systems locally, so I haven't tried to patch.
@civibot
Copy link

civibot bot commented Jun 18, 2018

(Standard links)

@eileenmcnaughton
Copy link
Contributor

I stepped through this and it makes sense to me. I can't see a risk with it. Merging

@eileenmcnaughton eileenmcnaughton merged commit 9f06cdc into civicrm:master Jun 19, 2018
@totten totten deleted the master-flush-prefix branch June 19, 2018 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants