• This my code:

    $albums = $wpdb->get_results( "SELECT id, name FROM $wpdb->wppa_albums", ARRAY_A );

    It produces 2 warnings:
    – Use of a direct database call is discouraged.
    – Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().

    What should i do with these messages? I have no idea how wp_cache_get() can be used here.

    Note: My plugin wp-photo-album-plus creates and maintains various own db tables, like wppa_albums

Viewing 2 replies - 1 through 2 (of 2 total)
  • These are warnings because it means you probably want to take a closer look at the code in question and decide whether they really apply in your use case or not. The checks are static as they don’t actually run your code, so they can’t know whether you are already using caching for example.

    Since you have your own custom database tables, there’s obviously no way around doing direct database calls, so I’d say you can ignore the first one.

    The caching recommendation depends very much on how your plugin is setup, what the code does, how often it runs etc. The idea behind it is that caching expensive queries in an external object cache is often times cheaper than fetching it from the database every single time.
    This particular query looks very simple though, so you probably don’t get any benefit from simply caching that one alone. But again, depends on the larger context. It can’t hurt to read up on the cache API documentation though to see for yourself 🙂

    Thread Starter Jacob N. Breetvelt

    (@opajaap)

    Thanx for your reply.

    I use caching query results by storing the result inside the function that does the query in a static var / static array of vars holding the rsults.

    Example:

    function wppa_get_imgevents( $type = '', $id = '', $no_popup = false, $idx = '' ) {
    global $wpdb;
    static $cache;
    .
    .
    .
    if ( wppa_switch( 'popup_text_ncomments' ) ) {
    	if ( isset( $cache[$id] ) ) {
    		$ncom = $cache[$id];
    	}
    	else {
    		$ncom = wppa_get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->wppa_comments WHERE photo = %d AND status = 'approved'", $id ) );
    		$cache[$id] = $ncom;
    	}
    }
    
    else $ncom = '0';
    if ( $ncom ) {
    	/* translators: integer count */
    	$ncom = sprintf( _n( '%d comment', '%d comments', $ncom, 'wp-photo-album-plus' ), $ncom );
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.