Untitled
unknown
php
2 years ago
1.9 kB
4
Indexable
function my_plugin_refresh_cache() { // Your code to retrieve the data goes here $data = array( 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' ); // Set a unique key for the cached data $cache_key = 'my_plugin_cache'; // Store the data in the cache with an expiration time of 1 hour wp_cache_set( $cache_key, $data, 'my_plugin_group', 3600 ); // Use the cached data echo $data['key1']; echo $data['key2']; echo $data['key3']; } // Schedule the event to refresh the cache every 1 hour function my_plugin_schedule_refresh_cache() { if ( ! wp_next_scheduled( 'my_plugin_refresh_cache_event' ) ) { wp_schedule_event( time(), 'hourly', 'my_plugin_refresh_cache_event' ); } } add_action( 'wp', 'my_plugin_schedule_refresh_cache' ); add_action( 'my_plugin_refresh_cache_event', 'my_plugin_refresh_cache' ); /** V tomto příkladu definujeme funkci my_plugin_refresh_cache(), která načte data a uloží je do mezipaměti s dobou platnosti 1 hodina. Poté definujeme naplánovanou událost my_plugin_refresh_cache_event, která spustí funkci my_plugin_refresh_cache() každou hodinu. K naplánování události použijeme funkci wp_schedule_event() uvnitř funkce my_plugin_schedule_refresh_cache(), která se spustí při načtení systému WordPress. Pomocí funkce wp_next_scheduled() zkontrolujeme, zda je událost již naplánována, a pokud ne, naplánujeme událost tak, aby se spouštěla každou hodinu s využitím hodinového intervalu. Poté přidáme funkci my_plugin_refresh_cache() jako zpětné volání pro událost my_plugin_refresh_cache_event pomocí funkce add_action(). Díky této implementaci bude mezipaměť obnovována každou hodinu naplánovanou událostí, nikoli při každém načtení stránky, což sníží dopad na výkon webu. *//
Editor is loading...