Skip to content

Add a transient cleaner tool#8

Merged
lusimeon merged 4 commits intodevelopfrom
feature/transient_cleaner
Apr 7, 2021
Merged

Add a transient cleaner tool#8
lusimeon merged 4 commits intodevelopfrom
feature/transient_cleaner

Conversation

@lusimeon
Copy link
Contributor

@lusimeon lusimeon commented Nov 30, 2020

Motivation

Rather than (or in addition to) set a lifetime to a transient, we often need to clean a transient when a post is saved to update the front-office. So this PR introduce a way to clean transients based on a user-input configuration like the one below.

[
  # Transient to clean on post update.
  'post' => [
    'all' => [
      'transient_key',
    ]
    'post_type_key' => [
      'transient_key',
      'transient_key_1',
    ]
  ],
  # Transient to clean on term update.
  'term' => [
    'all' => []
    'your_taxonomy_type_key' => []
  ],
  # Transient to clean on option update.
  'option' => [
    'all' => []
    'option_key' => []
  ],
]

Todo

  • Add unit and functionnal test.

@lusimeon lusimeon added the enhancement New feature or request label Nov 30, 2020
@lusimeon lusimeon requested a review from a team November 30, 2020 17:04
@lusimeon lusimeon force-pushed the feature/transient_cleaner branch from ec0fe51 to 841448f Compare December 14, 2020 11:28
@codecov
Copy link

codecov bot commented Dec 14, 2020

Codecov Report

Merging #8 (e3a9412) into develop (4b4e526) will increase coverage by 13.88%.
The diff coverage is 100.00%.

Impacted file tree graph

@@              Coverage Diff               @@
##             develop       #8       +/-   ##
==============================================
+ Coverage      25.35%   39.24%   +13.88%     
- Complexity       102      131       +29     
==============================================
  Files              5        6        +1     
  Lines            280      344       +64     
==============================================
+ Hits              71      135       +64     
  Misses           209      209               
Flag Coverage Δ Complexity Δ
unittests 39.24% <100.00%> (+13.88%) 0.00 <29.00> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
src/TransientCleaner.php 100.00% <100.00%> (ø) 29.00 <29.00> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b4e526...e3a9412. Read the comment docs.

Copy link
Contributor

@titouanmathis titouanmathis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really useful! 👍

I have some thoughts and questions:

  • the TransientCleaner might be used as a service when creating new transients, should it implement the singleton pattern?
  • as a service, we might need to push a specific transient key to a specific part of the config, I think we need more methods to achieve that:
// Usage
$transient_cleaner = TransientCleaner::getInstance();
$transient_cleaner->addPostConfig('my_transient_key', 'my_cpt_name');

// Definition 
public function addPostConfig( string $transient_key, $cpt_name = 'all' ) {
  $this->config['post'][$cpt_name][] = $transient_key;
}

@lusimeon lusimeon force-pushed the feature/transient_cleaner branch 2 times, most recently from 146d8ad to fff95f2 Compare January 18, 2021 15:24
@lusimeon lusimeon marked this pull request as ready for review January 18, 2021 15:25
@lusimeon
Copy link
Contributor Author

@studiometa/wordpress This PR is ready for review but #11 needs to be merged before merge this one.

@titouanmathis

as a service, we might need to push a specific transient key to a specific part of the config, I think we need more methods to achieve that

Thanks for your feedbacks, in my opinion most of the time the config will be set only one time (on app init), so specific push methods aren't needed.
Otherwise, the behaviour can be achieved with actuals methods. Are you agree?

$config = $cleaner->get_config();
$cleaner->set_config(
    array_merge($config, ['post' => […]]);
);

@lusimeon lusimeon force-pushed the feature/transient_cleaner branch from fff95f2 to 714de0e Compare March 15, 2021 16:49
@lusimeon lusimeon force-pushed the feature/transient_cleaner branch 2 times, most recently from 714de0e to e012023 Compare March 18, 2021 17:55
@perruche
Copy link
Contributor

perruche commented Apr 2, 2021

Does this implementation support a configuration with wildcard caracters, or do we have to manually register all the transient keys ?

When implementing ajax filters for exemple, transient key are generelly looking something like this: filters_transient_key_{request_params}_{language_code}. Could be usefull to be able to delete all transient with the matching pattern: filters_transient_key_

@titouanmathis
Copy link
Contributor

Does this implementation support a configuration with wildcard caracters, or do we have to manually register all the transient keys ?

When implementing ajax filters for exemple, transient key are generelly looking something like this: filters_transient_key_{request_params}_{language_code}. Could be usefull to be able to delete all transient with the matching pattern: filters_transient_key_

I think it does, as there is a strpos() used in the cleaning methods, but I am not sure.
It can be tested easily by adding a test to the TransientCleanerTest.php file 😉

@lusimeon
Copy link
Contributor Author

lusimeon commented Apr 2, 2021

I think it does, as there is a strpos() used in the cleaning methods, but I am not sure.

Exactly it does thanks to strpos() ;)

This PR still a WIP, I need to push some fixes to make it working with WordPress Gutenberg post creation, and to validate test suite.

Clean transients based on transient key and content save (post, term, option).
@lusimeon lusimeon force-pushed the feature/transient_cleaner branch from e012023 to bdd47ef Compare April 3, 2021 00:02
@lusimeon lusimeon force-pushed the feature/transient_cleaner branch from e4f2250 to f9e729d Compare April 6, 2021 17:38
@lusimeon lusimeon force-pushed the feature/transient_cleaner branch from f9e729d to c5f2493 Compare April 6, 2021 18:19
@lusimeon lusimeon requested a review from titouanmathis April 6, 2021 18:19
@lusimeon
Copy link
Contributor Author

lusimeon commented Apr 6, 2021

This PR still a WIP, I need to push some fixes to make it working with WordPress Gutenberg post creation, and to validate test suite.

I fix test suite and Gutenberg compatibility, so this PR isn't a WIP anymore. Please can you (re)review it one last time?

Copy link
Contributor

@perruche perruche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 👏 this is looking really good !

@titouanmathis titouanmathis deleted the feature/transient_cleaner branch July 14, 2021 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants