add: cache (WIP)

This commit is contained in:
Tobias Reisinger 2020-08-13 16:29:26 +02:00
parent f167f9caec
commit 6d37bd9734
26 changed files with 365 additions and 61 deletions
src/models

View file

@ -2,6 +2,7 @@
#include <string.h>
#include <sqlite3.h>
#include <cache.h>
#include <cJSON.h>
#include <logger.h>
#include <database.h>
@ -144,6 +145,9 @@ controller_save(controller_t *controller)
controller->id = sqlite3_last_insert_rowid(global_database);
}
}
cache_invalidate_controller(controller->id);
return result;
}
@ -183,9 +187,15 @@ controller_free_list(controller_t **controllers)
free(controllers);
}
cJSON*
char*
controller_to_json(controller_t *controller)
{
char *cached = cache_get_json_controller(controller->id);
if(cached)
{
return cached;
}
char uuid_str[UUID_STR_LEN];
uuid_unparse(controller->uid, uuid_str);
@ -245,12 +255,17 @@ controller_to_json(controller_t *controller)
cJSON *json_relays = cJSON_CreateArray();
for(int i = 0; relays[i] != NULL; ++i)
{
cJSON_AddItemToArray(json_relays, relay_to_json(relays[i]));
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(relays[i]));
cJSON_AddItemToArray(json_relays, json_relay);
}
cJSON_AddItemToObject(json, "relays", json_relays);
relay_free_list(relays);
return json;
char *result = cJSON_Print(json);
cJSON_Delete(json);
cache_put_json_controller(controller->id, result);
return result;
}
controller_t*