How to manage resources during each config/show command?

Assume I have the following data model, value of a, b and c will be changed frequently(so, trans.init() is suitable for fetching values), we have a function get_ct(&ct_info) to fetch the value of a,b,and c, value of a, b, and c must be grouped.

container ct {
    leaf a;
    leaf b;
    leaf c;
}

the show command is

# show ct
ct a xxxx
ct b xxxx
ct c xxxx

I register the get_elem callback for container ct

int get_elem_ct(struct confd_trans_ctx *tctx, wconfd_hkeypath_t *kp)
{
    confd_value_t v;
    ct_info_t ct_info;
    switch(CONFD_GET_XMLTAG(&kp->v[0][0])
    {    
    case _a:
        get_ct_info(&ct_info);
        CONFD_SET_STR(&v, ct_info.a);  
        free_ct_info(&ct_info);
        break;
    case _b:
        get_ct_info(&ct_info);
        CONFD_SET_STR(&v, ct_info.b); 
        free_ct_info(&ct_info); 
        break;
    case _c:
        get_ct_info(&ct_info);
        CONFD_SET_STR(&v, ct_info.c);  
        free_ct_info(&ct_info);
        break;
    }
    confd_data_reply_value(tctx, &v);
    return CONFD_OK;
}

First because values are changed frequently I couldn’t cached them in global var I must fetch each time show command called, second, because I couldn’t fill all the leafs value in one swoop, the guide says that get_object() callback will only be called for list entries. I have to write code to fill each leaf like above. The problem is

  1. a, b, c values are not grouped any more. for example we call get_ct_info() there times

    call_id a b c
    1 1 2 3
    2 4 5 6
    3 7 8 9

    show ct

    ct a 1
    ct b 5
    ct c 9

this is the wrong result, which should be

ct a 1
ct b 2
ct c 3

or

ct a 4
ct b 5
ct c 6

or

ct a 7
ct b 8
ct c 9
  1. we invoke get_ct_info() too many times, assume get_ct_info() should connect to other process by tcp, if the resource need much time and memory we couldn’t accept this thing happens.

Is there a way for us to prepare resource at the beginning of show command, and release the resource at the end of show command.

  1. Consider defining your data model as a list instead of a container, perhaps without keys, and implement get_object( ) instead of get_elem( ) to insure that all three elements that go together are always being returned at the same time.

  2. Consider setting a small cache value to insure that get_ct_info( ) isn’t being invoked too many times within a short time.

Defining the data model as a list could solve the problem, but not that grace.
I think that It is better if ConfD could add two callbacks in struct confd_data_cbs one is invoked at the beginning of command executed and the other is invoked after the command finished.