add: cache (WIP)
This commit is contained in:
parent
f167f9caec
commit
6d37bd9734
26 changed files with 365 additions and 61 deletions
|
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required (VERSION 3.7)
|
||||
project(core
|
||||
VERSION 0.2.6
|
||||
VERSION 0.2.7
|
||||
LANGUAGES C)
|
||||
|
||||
add_executable(core src/main.c)
|
||||
|
|
|
|||
|
|
@ -3,9 +3,52 @@
|
|||
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include <models/schedule.h>
|
||||
#include <models/relay.h>
|
||||
#include <models/controller.h>
|
||||
|
||||
extern sqlite3 *cache_database;
|
||||
|
||||
void
|
||||
cache_init();
|
||||
|
||||
void
|
||||
cache_free();
|
||||
|
||||
|
||||
void
|
||||
cache_put_json_schedule(int schedule_id, char *schedule_json);
|
||||
|
||||
char*
|
||||
cache_get_json_schedule(int schedule_id);
|
||||
|
||||
void
|
||||
cache_invalidate_schedule(int schedule_id);
|
||||
|
||||
|
||||
void
|
||||
cache_put_json_relay(int relay_id, char *relay_json);
|
||||
|
||||
char*
|
||||
cache_get_json_relay(int relay_id);
|
||||
|
||||
void
|
||||
cache_invalidate_relay(int relay_id);
|
||||
|
||||
|
||||
|
||||
void
|
||||
cache_put_json_controller(int controller_id, char *controller_json);
|
||||
|
||||
char*
|
||||
cache_get_json_controller(int controller_id);
|
||||
|
||||
void
|
||||
cache_invalidate_controller(int controller_id);
|
||||
|
||||
|
||||
|
||||
void
|
||||
cache_invalidate_tagged(int tag_id);
|
||||
|
||||
#endif /* CORE_CACHE_H */
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
#include <sqlite3.h>
|
||||
|
||||
#include <constants.h>
|
||||
#include <cJSON.h>
|
||||
#include <helpers.h>
|
||||
#include <models/relay.h>
|
||||
|
||||
|
|
@ -32,7 +31,7 @@ controller_save(controller_t* contoller);
|
|||
int
|
||||
controller_remove(controller_t* contoller);
|
||||
|
||||
cJSON*
|
||||
char*
|
||||
controller_to_json(controller_t* contoller);
|
||||
|
||||
controller_t*
|
||||
|
|
|
|||
|
|
@ -19,9 +19,6 @@ junction_tag_insert(int tag_id, int relay_id, int schedule_id);
|
|||
int
|
||||
junction_tag_remove(int tag_id, int relay_id, int schedule_id);
|
||||
|
||||
int
|
||||
junction_tag_remove_for_tag(int tag_id);
|
||||
|
||||
int
|
||||
junction_tag_remove_for_relay(int relay_id);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
#include <uuid/uuid.h>
|
||||
|
||||
#include <constants.h>
|
||||
#include <cJSON.h>
|
||||
#include <helpers.h>
|
||||
#include <database.h>
|
||||
#include <models/schedule.h>
|
||||
|
|
@ -28,10 +27,10 @@ relay_save();
|
|||
void
|
||||
relay_reload_active_schedule(relay_t *relay);
|
||||
|
||||
cJSON*
|
||||
char*
|
||||
relay_to_json(relay_t *relay);
|
||||
|
||||
cJSON*
|
||||
char*
|
||||
relay_list_to_json(relay_t **relays);
|
||||
|
||||
void
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
#include <uuid/uuid.h>
|
||||
|
||||
#include <cJSON.h>
|
||||
#include <constants.h>
|
||||
#include <models/period.h>
|
||||
|
||||
|
|
@ -31,7 +30,7 @@ schedule_free(schedule_t *schedule);
|
|||
void
|
||||
schedule_free_list(schedule_t **schedule);
|
||||
|
||||
cJSON*
|
||||
char*
|
||||
schedule_to_json(schedule_t *schedule);
|
||||
|
||||
void
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
CREATE TABLE `cache` (
|
||||
`key` STRING PRIMARY KEY,
|
||||
`value` TEXT NOT NULL,
|
||||
`expiration` INT DEFAULT 0
|
||||
CREATE TABLE cache (
|
||||
key STRING
|
||||
PRIMARY KEY,
|
||||
value TEXT
|
||||
NOT NULL,
|
||||
expiration INT
|
||||
DEFAULT 0
|
||||
);
|
||||
|
|
|
|||
203
src/cache.c
203
src/cache.c
|
|
@ -1,9 +1,86 @@
|
|||
#include <cache.h>
|
||||
#include <logger.h>
|
||||
#include <sql/cache.h>
|
||||
#include <models/junction_tag.h>
|
||||
|
||||
|
||||
sqlite3 *cache_database;
|
||||
|
||||
static char*
|
||||
cache_get_value(char *key)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
sqlite3_prepare_v2(cache_database, "SELECT value FROM cache WHERE key=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
|
||||
|
||||
char *result = NULL;
|
||||
|
||||
while(1)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
const char *found_value = (const char *)sqlite3_column_text(stmt, 0);
|
||||
result = (char*)malloc(sizeof(char) * (strlen(found_value) + 1));
|
||||
strcpy(result, found_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_WARNING("failed selecting %s from cache: %s\n", key, sqlite3_errstr(s));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
cache_insert_value(char *key, char *value)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
sqlite3_prepare_v2(cache_database, "INSERT INTO cache (key, value, expiration) VALUES (?1, ?2, 0);", -1, &stmt, NULL);
|
||||
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, value, -1, SQLITE_STATIC);
|
||||
|
||||
int rc = sqlite3_step(stmt);
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if(rc != SQLITE_DONE)
|
||||
{
|
||||
LOGGER_WARNING("failed to save %s to cache: %s\n", key, sqlite3_errstr(rc));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
cache_invalidate(char *key)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
sqlite3_prepare_v2(cache_database, "DELETE FROM cache WHERE key=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if(rc != SQLITE_DONE)
|
||||
{
|
||||
LOGGER_WARNING("failed to invalidate %s in cache: %s\n", key, sqlite3_errstr(rc));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cache_init()
|
||||
{
|
||||
|
|
@ -12,6 +89,130 @@ cache_init()
|
|||
if(rc)
|
||||
{
|
||||
LOGGER_CRIT("can't open cache database: %s\n", sqlite3_errmsg(cache_database));
|
||||
return;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char* err_msg;
|
||||
rc = sqlite3_exec(cache_database, (const char *)sql_cache_sql, NULL, NULL, &err_msg);
|
||||
if(rc)
|
||||
{
|
||||
LOGGER_CRIT("couldn't initialize cache (%s)\n", err_msg);
|
||||
exit(1);
|
||||
}
|
||||
LOGGER_DEBUG("cache initalized\n");
|
||||
}
|
||||
|
||||
void
|
||||
cache_free()
|
||||
{
|
||||
sqlite3_close(cache_database);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
cache_put_json_schedule(int schedule_id, char *schedule_json)
|
||||
{
|
||||
char key[32];
|
||||
sprintf(key, "schedule_json:%d", schedule_id);
|
||||
cache_insert_value(key, schedule_json);
|
||||
}
|
||||
|
||||
char*
|
||||
cache_get_json_schedule(int schedule_id)
|
||||
{
|
||||
char key[32];
|
||||
sprintf(key, "schedule_json:%d", schedule_id);
|
||||
return cache_get_value(key);
|
||||
}
|
||||
|
||||
void
|
||||
cache_invalidate_schedule(int schedule_id)
|
||||
{
|
||||
char key[32];
|
||||
sprintf(key, "schedule_json:%d", schedule_id);
|
||||
cache_invalidate(key);
|
||||
|
||||
relay_t **relays = relay_get_with_schedule(schedule_id);
|
||||
|
||||
for(int i = 0; relays[i] != NULL; ++i)
|
||||
{
|
||||
cache_invalidate_relay(relays[i]->id);
|
||||
}
|
||||
|
||||
relay_free_list(relays);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
cache_put_json_relay(int relay_id, char *relay_json)
|
||||
{
|
||||
char key[32];
|
||||
sprintf(key, "relay_json:%d", relay_id);
|
||||
cache_insert_value(key, relay_json);
|
||||
}
|
||||
|
||||
char*
|
||||
cache_get_json_relay(int relay_id)
|
||||
{
|
||||
char key[32];
|
||||
sprintf(key, "relay_json:%d", relay_id);
|
||||
return cache_get_value(key);
|
||||
}
|
||||
|
||||
void
|
||||
cache_invalidate_relay(int relay_id)
|
||||
{
|
||||
char key[32];
|
||||
sprintf(key, "relay_json:%d", relay_id);
|
||||
cache_invalidate(key);
|
||||
|
||||
relay_t *relay = relay_get_by_id(relay_id);
|
||||
cache_invalidate_controller(relay->controller_id);
|
||||
relay_free(relay);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
cache_put_json_controller(int controller_id, char *controller_json)
|
||||
{
|
||||
char key[32];
|
||||
sprintf(key, "controller_json:%d", controller_id);
|
||||
cache_insert_value(key, controller_json);
|
||||
}
|
||||
|
||||
char*
|
||||
cache_get_json_controller(int controller_id)
|
||||
{
|
||||
char key[32];
|
||||
sprintf(key, "controller_json:%d", controller_id);
|
||||
return cache_get_value(key);
|
||||
}
|
||||
|
||||
void
|
||||
cache_invalidate_controller(int controller_id)
|
||||
{
|
||||
char key[32];
|
||||
sprintf(key, "controller_json:%d", controller_id);
|
||||
cache_invalidate(key);
|
||||
}
|
||||
|
||||
void
|
||||
cache_invalidate_tagged(int tag_id)
|
||||
{
|
||||
int *schedule_ids = junction_tag_get_schedules_for_tag_id(tag_id);
|
||||
for(int i = 0; schedule_ids[i] != 0; ++i)
|
||||
{
|
||||
cache_invalidate_schedule(schedule_ids[i]);
|
||||
}
|
||||
free(schedule_ids);
|
||||
|
||||
int *relay_ids = junction_tag_get_relays_for_tag_id(tag_id);
|
||||
for(int i = 0; relay_ids[i] != 0; ++i)
|
||||
{
|
||||
cache_invalidate_relay(relay_ids[i]);
|
||||
}
|
||||
free(relay_ids);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ database_migrate()
|
|||
case 0:
|
||||
LOGGER_INFO("migrating LEVEL 0\n");
|
||||
rc = sqlite3_exec(global_database, (const char *)sql_migration_0_sql, NULL, NULL, &err_msg);
|
||||
if(rc != 0)
|
||||
if(rc)
|
||||
{
|
||||
LOGGER_CRIT("couldn't migrate LEVEL 0 (%s)\n", err_msg);
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ api_v1_controllers_GET(struct mg_connection *nc, struct http_message *hm, endpoi
|
|||
|
||||
for(int i = 0; all_controllers[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_controller = controller_to_json(all_controllers[i]);
|
||||
cJSON *json_controller = cJSON_CreateRaw(controller_to_json(all_controllers[i]));
|
||||
|
||||
cJSON_AddItemToArray(json, json_controller);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, en
|
|||
return;
|
||||
}
|
||||
|
||||
cJSON *json = controller_to_json(controller);
|
||||
cJSON *json = cJSON_CreateRaw(controller_to_json(controller));
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
|
|
@ -140,7 +140,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
|
|||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = controller_to_json(controller);
|
||||
json = cJSON_CreateRaw(controller_to_json(controller));
|
||||
|
||||
command_set_controller_name(controller);
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *nc, struct http_message
|
|||
|
||||
for(int i = 0; all_relays[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_relay = relay_to_json(all_relays[i]);
|
||||
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(all_relays[i]));
|
||||
|
||||
cJSON_AddItemToArray(json, json_relay);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess
|
|||
return;
|
||||
}
|
||||
|
||||
cJSON *json = relay_to_json(relay);
|
||||
cJSON *json = cJSON_CreateRaw(relay_to_json(relay));
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
|
|
@ -220,7 +220,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
|||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = relay_to_json(relay);
|
||||
json = cJSON_CreateRaw(relay_to_json(relay));
|
||||
|
||||
command_set_relay_schedule(relay);
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ api_v1_relays_GET(struct mg_connection *nc, struct http_message *hm, endpoint_ar
|
|||
|
||||
for(int i = 0; all_relays[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_relay = relay_to_json(all_relays[i]);
|
||||
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(all_relays[i]));
|
||||
|
||||
cJSON_AddItemToArray(json, json_relay);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ api_v1_relays_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, end
|
|||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *json_relay = relay_to_json(relay);
|
||||
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(relay));
|
||||
|
||||
cJSON_AddItemToArray(json, json_relay);
|
||||
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
|
|||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = schedule_to_json(new_schedule);
|
||||
json = cJSON_CreateRaw(schedule_to_json(new_schedule));
|
||||
|
||||
endpoint_response_json(response, 201, json);
|
||||
cJSON_Delete(json);
|
||||
|
|
@ -165,7 +165,7 @@ api_v1_schedules_GET(struct mg_connection *nc, struct http_message *hm, endpoint
|
|||
|
||||
for(int i = 0; all_schedules[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_schedule = schedule_to_json(all_schedules[i]);
|
||||
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(all_schedules[i]));
|
||||
|
||||
cJSON_AddItemToArray(json, json_schedule);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endp
|
|||
return;
|
||||
}
|
||||
|
||||
cJSON *json = schedule_to_json(schedule);
|
||||
cJSON *json = cJSON_CreateRaw(schedule_to_json(schedule));
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
|
|
@ -176,7 +176,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp
|
|||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = schedule_to_json(schedule);
|
||||
json = cJSON_CreateRaw(schedule_to_json(schedule));
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ api_v1_schedules_tag_STR_GET(struct mg_connection *nc, struct http_message *hm,
|
|||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *json_schedule = schedule_to_json(schedule);
|
||||
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(schedule));
|
||||
|
||||
cJSON_AddItemToArray(json, json_schedule);
|
||||
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_
|
|||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *json_relay = relay_to_json(relay);
|
||||
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(relay));
|
||||
|
||||
cJSON_AddItemToArray(json_relays, json_relay);
|
||||
|
||||
|
|
@ -66,7 +66,7 @@ api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_
|
|||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *json_schedule = schedule_to_json(schedule);
|
||||
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(schedule));
|
||||
|
||||
cJSON_AddItemToArray(json_schedules, json_schedule);
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
#include <mongoose.h>
|
||||
#include <confini.h>
|
||||
|
||||
#include <cache.h>
|
||||
#include <router.h>
|
||||
#include <logger.h>
|
||||
#include <config.h>
|
||||
|
|
@ -30,6 +31,7 @@ terminate(int signum)
|
|||
|
||||
router_free();
|
||||
status_free();
|
||||
cache_free();
|
||||
|
||||
closelog();
|
||||
|
||||
|
|
@ -54,7 +56,6 @@ main(int argc, const char** argv)
|
|||
setlogmask(LOG_UPTO(LOG_INFO));
|
||||
|
||||
|
||||
|
||||
/******************** LOAD CONFIG ********************/
|
||||
|
||||
global_config.file = "core.ini";
|
||||
|
|
@ -152,6 +153,7 @@ main(int argc, const char** argv)
|
|||
|
||||
/******************** INIT COMPONENTS ********************/
|
||||
|
||||
cache_init();
|
||||
router_init();
|
||||
status_init();
|
||||
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue