created a new configuration caching API (config_changed).

applied this to some rules.
This commit is contained in:
Enno Rehling 2016-09-11 12:15:53 +02:00
parent aeaa96f026
commit f75be76ee1
3 changed files with 46 additions and 2 deletions

View file

@ -887,14 +887,23 @@ int rule_blessed_harvest(void)
int rule_alliance_limit(void) int rule_alliance_limit(void)
{ {
int rule = config_get_int("rules.limit.alliance", 0); static int cache_token;
static int rule = 0;
if (config_changed(&cache_token)) {
rule = config_get_int("rules.limit.alliance", 0);
}
assert(rule >= 0); assert(rule >= 0);
return rule; return rule;
} }
int rule_faction_limit(void) int rule_faction_limit(void)
{ {
int rule = config_get_int("rules.limit.faction", 0); static int cache_token;
static int rule = 0;
if (config_changed(&cache_token)) {
rule = config_get_int("rules.limit.faction", 0);
}
assert(rule >= 0); assert(rule >= 0);
return rule; return rule;
} }
@ -1053,8 +1062,19 @@ bool markets_module(void)
} }
static struct param *configuration; static struct param *configuration;
static int config_cache_key = 1;
bool config_changed(int *cache_key) {
assert(cache_key);
if (config_cache_key != *cache_key) {
*cache_key = config_cache_key;
return true;
}
return false;
}
void config_set(const char *key, const char *value) { void config_set(const char *key, const char *value) {
++config_cache_key;
set_param(&configuration, key, value); set_param(&configuration, key, value);
} }

View file

@ -184,6 +184,7 @@ struct param;
int config_get_int(const char *key, int def); int config_get_int(const char *key, int def);
double config_get_flt(const char *key, double def); double config_get_flt(const char *key, double def);
bool config_token(const char *key, const char *tok); bool config_token(const char *key, const char *tok);
bool config_changed(int *cache_key);
char * join_path(const char *p1, const char *p2, char *dst, size_t len); char * join_path(const char *p1, const char *p2, char *dst, size_t len);
bool ExpensiveMigrants(void); bool ExpensiveMigrants(void);

View file

@ -190,9 +190,31 @@ static void test_default_order(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_config_cache(CuTest *tc) {
int key = 0;
test_setup();
CuAssertTrue(tc, config_changed(&key));
config_set("hodor", "0");
CuAssertTrue(tc, config_changed(&key));
CuAssertTrue(tc, !config_changed(&key));
test_cleanup();
}
static void test_rules(CuTest *tc) {
CuAssertIntEquals(tc, 0, rule_alliance_limit());
config_set("rules.limit.alliance", "1");
CuAssertIntEquals(tc, 1, rule_alliance_limit());
CuAssertIntEquals(tc, 0, rule_faction_limit());
config_set("rules.limit.faction", "1000");
CuAssertIntEquals(tc, 1000, rule_faction_limit());
}
CuSuite *get_config_suite(void) CuSuite *get_config_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_config_cache);
SUITE_ADD_TEST(suite, test_get_set_param); SUITE_ADD_TEST(suite, test_get_set_param);
SUITE_ADD_TEST(suite, test_param_int); SUITE_ADD_TEST(suite, test_param_int);
SUITE_ADD_TEST(suite, test_param_flt); SUITE_ADD_TEST(suite, test_param_flt);
@ -200,5 +222,6 @@ CuSuite *get_config_suite(void)
SUITE_ADD_TEST(suite, test_getunit); SUITE_ADD_TEST(suite, test_getunit);
SUITE_ADD_TEST(suite, test_read_unitid); SUITE_ADD_TEST(suite, test_read_unitid);
SUITE_ADD_TEST(suite, test_default_order); SUITE_ADD_TEST(suite, test_default_order);
SUITE_ADD_TEST(suite, test_rules);
return suite; return suite;
} }