Dirty wordpress and my large db options

Per diversi motivi ho effettuato alcune installazioni di wordpress ,e anche se sempre mantenute aggiornate , ogni tanto qualcosa non va.

Mi trovo un’installazione che ha problemi nel fare il backup del db.

Penso che si sia inceppato qualcosa , questa installazione è simile ad altre che non hanno problemi e incomincio a girare a zonzo tra i plugin senza venirne a capo.

Non avevo ancora prestato attenzione alla dimensione del db che phpmyadmin mi mostrava in bella vista.

C’è un piccolo problema nel comprimere un db di 168 Mb !!!

pettes

Effettuo un paio di ricerche i cui primi indizi sono utili come un paio di calzini usati.

Qualcosa è cambiato in wordpresse e per ottimizzare la cache si fa ricorso alle trancient API, ma quelle ‘expired‘ non vengono rimosse in automatico in questo caso.

Passiamo ora alla pulizia.

Un buon articolo che mi ha dato informazioni utili é questo : Does WordPress delete expired transients from the database?

So why are transients not being deleted automatically?

A little googling brought me to a post from WPEngine on the subject;

But with the WordPress Transients, you get different but still very undesirable behavior. Because the values are written to the database, not a fixed-sized block of RAM, they all stick around. Which means even with the heavily-loaded site, you still have your session data. Awesome!

Or so you thought. Because what isn’t said in the Transient API is what happens when you use unique keys like sessions. And what happens in with the built-in method is that the options table fills up indefinitely! Because: WordPress’s “old data clean up” only operates when you request the key (as we covered earlier). If you just leave the key, it’s left in the options table, forever. There’s no separate process that cleans these up!

More at A Technical Transients Treatise at WPEngine

  • Soluzione 1

Aggiungere il plugin Delete Expired Transients per rimuovere manualmente la roba vecchia che persiste nel database.

Plugin recente ( 1.0.0 ) e compatibile dalla versione 3.6 che però non risolve il problema del backup schedulato.

  • Soluzione 2

Decido di provare un nuovo plugin che però non si installa in automatico da wordpress , ma che automaticametne mi cancella gli ‘expired trancients’ , e creo nella directory plugin di wordpress il file :purge-transients.php

Qui è possibile trovare la versione su github : https://github.com/Seebz/Snippets/tree/master/Wordpress/plugins/purge-transients

Per i più sfaticati ne lascio una copia di quello che uso io , dove ho impostato la pulizia ogni 2 giorni , dopo aver aggiunto il file occorre , per abilitare il plugin , andare nella scheda plugin installati e abilitare Purge Transients.

Ecchilo :

<?php
/*
Plugin Name: Purge Transients
Description: Purge old transients
Version: 0.2.1
Author: Seebz
*/

if ( ! function_exists('purge_transients') ) {
function purge_transients($older_than = '2 days', $safemode = true) {
global $wpdb;

$older_than_time = strtotime('-' . $older_than);
if ($older_than_time > time() || $older_than_time < 1) {
return false;
}

$transients = $wpdb->get_col(
$wpdb->prepare( "
SELECT REPLACE(option_name, '_transient_timeout_', '') AS transient_name
FROM {$wpdb->options}
WHERE option_name LIKE '\_transient\_timeout\__%%'
AND option_value < %s
", $older_than_time)
);
if ($safemode) {
foreach($transients as $transient) {
get_transient($transient);
}
} else {
$options_names = array();
foreach($transients as $transient) {
$options_names[] = '_transient_' . $transient;
$options_names[] = '_transient_timeout_' . $transient;
}
if ($options_names) {
$options_names = array_map(array($wpdb, 'escape'), $options_names);
$options_names = "'". implode("','", $options_names) ."'";

$result = $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name IN ({$options_names})" );
if (!$result) {
return false;
}
}
}

return $transients;
}
}

function purge_transients_activation () {
if (!wp_next_scheduled('purge_transients_cron')) {    
wp_schedule_event( time(), 'daily', 'purge_transients_cron');
}
}
register_activation_hook(__FILE__, 'purge_transients_activation');

function purge_transients_deactivation () {
if (wp_next_scheduled('purge_transients_cron')) {
wp_clear_scheduled_hook('purge_transients_cron');
}
}
register_deactivation_hook(__FILE__, 'purge_transients_deactivation');

function do_purge_transients_cron () {
purge_transients();
}
add_action('purge_transients_cron', 'do_purge_transients_cron');

?>

Se volete modificare i giorni per la pulizia , il valore da modificare si trova in questa stringa :

if ( ! function_exists('purge_transients') ) {
function purge_transients($older_than = '2 days', $safemode = true) {
global $wpdb;

adesso potete lasciare il vostro blog tranquillo a riposare

Rispondi

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.