0

I am looking to add a pre-built options list, based on a query. I crafted the function nicely with the below code

function mitacan_webform_select_options_info() {
    $items = [];
    if (function_exists('basliklariAl')) {
        $items['addresses'] = array(
            'title' => 'Stok Kodları',
            'options callback' => 'basliklariAl'
        );
    }
    return $items;
}

function basliklariAl($component, $flat, $arguments) {
    $nodes = [];
    $select = db_select('node','n')
        ->fields('n',[
            'nid',
            'title'
        ])
        ->condition('type','urun')
        ->execute()
        ->fetchAll();
    foreach($select as $n) {
        $nodes[$n->nid] = $n->title;
    }
    return $nodes;
}

This is very slow, mostly because there are about 10k records. What can I do to optimise the process?

  • How often do you expect the query results to change ? Why don't you use cache_set and cache_get ? – GiorgosK Mar 9 '18 at 9:00
  • I consume a web service to generate the nodes once a day, but expect the results to change rarely. – Kartagis Mar 9 '18 at 9:02
0

You can always cache the results as seen in the official documentation

  if ($cache = cache_get('my_module_cache') && REQUEST_TIME < $cache->expire) {
    $data = $cache->data;
  }
  else {
    $data = 'xxxxxxx'; //do all your time taking complex calculations here
    cache_set('my_module_cache', $data, 'cache',  REQUEST_TIME  + (3600 * 24 * 30 * 6)); //stores in cache table and expires after 6 months
  }

More about cache_set and cache_get

  • I added the cache_set but the cache looks like +---------------+------+--------+------------+------------+ | cid | data | expire | created | serialized | +---------------+------+--------+------------+------------+ | mitacan_cache | N; | 86400 | 1520589086 | 1 | +---------------+------+--------+------------+------------+. What did I do wrong? – Kartagis Mar 9 '18 at 10:01
  • Sorry the code was untested as I was just trying to give you a headstart ... I updated my answer with the official documentation example now that should probably work better ... but again its untested – GiorgosK Mar 9 '18 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.