Opened 3 years ago

Closed 3 years ago

#33060 closed Cleanup/optimization (fixed)

Add a helper function to make and validate cache keys.

Reported by: Nick Pope Owned by: Nick Pope
Component: Core (Cache system) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Following from this thread the following pattern is repeated a lot in the cache backends:

        key = self.make_key(key, version=version)
        self.validate_key(key)

We can define a helper function on the base cache backend that can be used to avoid repetitiveness and help ensure that we consistently call .validate_key() after .make_key():

    def make_and_validate_key(self, key, version=None):
        key = self.make_key(key, version=version)
        self.validate_key(key)
        return key

An alternative proposal is to have .make_key() learn a validate flag, but we'd probably need to have it as False by default for backward compatibility and we'd may still have issues if users have overridden .make_key(). So it would require documentation changes, release notes, and a deprecation period.

Change History (8)

comment:1 by Nick Pope, 3 years ago

Has patch: set

comment:2 by Mariusz Felisiak, 3 years ago

Triage Stage: UnreviewedAccepted

Adding make_and_validate_key() sounds reasonable.

comment:3 by Mariusz Felisiak, 3 years ago

Needs tests: set
Patch needs improvement: set

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 68b8eda7:

Refs #33060 -- Added .make_key() in .touch() for dummy cache backend.

All cache operations should use make_key().

comment:5 by Mariusz Felisiak, 3 years ago

Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In ec2f6ea9:

Refs #33060 -- Added memcached test for make_key() making keys invalid.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 4b82578a:

Refs #33060 -- Ensured cache backends validate keys.

The validate_key() function should be called after make_key() to ensure
that the validation is performed on the key that will actually be
stored in the cache.

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@…>

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 42dfa97e:

Fixed #33060 -- Added BaseCache.make_and_validate_key() hook.

This helper function reduces the amount of duplicated code and makes it
easier to ensure that we always validate the keys.

Note: See TracTickets for help on using tickets.
Back to Top