diff --git a/TODO.txt b/TODO.txt index 849576c..41e813f 100644 --- a/TODO.txt +++ b/TODO.txt @@ -14,7 +14,8 @@ Due: January 1, 2023 [~] Data abstraction layer [ ] Database upgrades/migration path - [ ] Caching and cache control + [x] Caching + [ ] Cache eviction [x] Make memory info access O(1) [x] Make config option 'id' optional; print warning if not present started as root. [x] Write install and uninstall scripts diff --git a/src/Db.c b/src/Db.c index f7d45f1..987d800 100644 --- a/src/Db.c +++ b/src/Db.c @@ -330,8 +330,6 @@ DbLock(Db * db, char *prefix, char *key) ref->json = json; ref->ts = diskTs; ref->size = DbComputeSize(ref->json); - ref->prefix = UtilStringDuplicate(prefix); - ref->key = UtilStringDuplicate(key); } } else @@ -365,8 +363,8 @@ DbLock(Db * db, char *prefix, char *key) pthread_mutex_init(&ref->lock, NULL); ref->ts = UtilServerTs(); ref->size = DbComputeSize(ref->json); - ref->prefix = prefix; - ref->key = key; + ref->prefix = UtilStringDuplicate(prefix); + ref->key = UtilStringDuplicate(key); /* If cache is enabled, cache this ref */ if (db->cache) @@ -389,6 +387,7 @@ finish: Free(file); Free(hash); + return ref; } @@ -407,8 +406,11 @@ DbUnlock(Db * db, DbRef * ref) file = DbFileName(db, ref->prefix, ref->key); fp = fopen(file, "w"); + Free(file); + if (!fp) { + pthread_mutex_unlock(&db->lock); return 0; } @@ -427,7 +429,15 @@ DbUnlock(Db * db, DbRef * ref) pthread_mutex_destroy(&ref->lock); Free(ref); } + else + { + /* This ref should be in the cache, just update it's size */ + db->cacheSize -= ref->size; + ref->size = DbComputeSize(ref->json); + db->cacheSize += ref->size; + } + pthread_mutex_unlock(&db->lock); return 1; } diff --git a/src/include/Json.h b/src/include/Json.h index 144eda6..68e5993 100644 --- a/src/include/Json.h +++ b/src/include/Json.h @@ -151,6 +151,9 @@ extern char * extern JsonValue * JsonValueInteger(long integer); +extern long + JsonValueAsInteger(JsonValue *); + extern JsonValue * JsonValueFloat(double floating);