<!--#
Test type 7: Caching
TechEmpower Web Framework Benchmarks
-->
<!--#declaration
#include "world.h"

static UCache* cache;
static World* pworld_query;

#ifndef AS_cpoll_cppsp_DO
static UVector<World*>* pvworld_query;
#endif

static void usp_init_cached_worlds()
{
   U_TRACE(5, "::usp_init_cached_worlds()")

   UOrmSession sql_query(U_CONSTANT_TO_PARAM("hello_world"));

   if (sql_query.isReady() == false)
      {
      U_WARNING("usp_init_cached_worlds(): we cound't connect to db");

      return;
      }

   U_NEW(World, pworld_query, World);

   UOrmStatement stmt_query(sql_query, U_CONSTANT_TO_PARAM("SELECT * FROM World"));

   stmt_query.into(*pworld_query);

   stmt_query.execute();

   // creat and fill the cache

   U_NEW(UCache, cache, UCache);

   (void) cache->open(U_STRING_FROM_CONSTANT("/tmp/ULib_cached_worlds.cache"), 10000 * 64, U_NULLPTR, true);

   do {
      cache->add(pworld_query->id, pworld_query->randomNumber);
      }
   while (stmt_query.nextRow());

#ifndef AS_cpoll_cppsp_DO
   U_NEW(UVector<World*>, pvworld_query, UVector<World*>(500));
#endif
}

#ifdef DEBUG
static void usp_end_cached_worlds()
{
   U_TRACE(5, "::usp_end_cached_worlds()")

   if (cache)
      {
      delete cache;
      delete pworld_query;

#  ifndef AS_cpoll_cppsp_DO
      delete pvworld_query;
#  endif
      }
}
#endif
-->
<!--#header
Content-Type: application/json
-->
<!--#code
int i = 0, num_queries = UHTTP::getFormFirstNumericValue(1, 500);

#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR('[');
#endif

while (true)
   {
   pworld_query->randomNumber = cache->getNumber((pworld_query->id = u_get_num_random(10000-1)));

#ifdef AS_cpoll_cppsp_DO
   USP_PRINTF("{\"id\":%u,\"randomNumber\":%u}", pworld_query->id, pworld_query->randomNumber);
#else
   World* pworld;

   U_NEW(World, pworld, World(*pworld_query));

   pvworld_query->push_back(pworld);
#endif

   if (++i == num_queries) break;

#ifdef AS_cpoll_cppsp_DO
   USP_PUTS_CHAR(',');
#endif
   }

#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR(']');
#else
USP_OBJ_JSON_stringify(*pvworld_query);
pvworld_query->clear();
#endif
-->
