diff --git a/src/common.h b/src/common.h index b942997..074a3d8 100644 --- a/src/common.h +++ b/src/common.h @@ -5,6 +5,7 @@ extern sig_atomic_t misses; extern sig_atomic_t hits; extern sig_atomic_t connections; +extern int record_expire_time; KCDB* db; int server; QUEUE requests; diff --git a/src/handlers.c b/src/handlers.c index d7f24e9..d2f69f5 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -83,14 +83,31 @@ void handle_get(KCLIST* tokens, FILE* client){ ++misses; sprintf(out, "VALUE %s 0 0\r\n\r\nEND\r\n", key); } else{ - ++hits; - sprintf(out, "VALUE %s 0 %d\r\n%s\r\nEND\r\n", key, (int)strlen(result_buffer), result_buffer); + char* value = result_buffer + 11; + char expiration[16]; + strncpy(expiration, result_buffer, 10); + int exp = atoi(expiration); + int now = (int)time(NULL); + if(exp > 0 && exp < now){ + ++misses; + value = ""; + char reset_value[16]; + sprintf(reset_value, "%10d:0", 0); + kcdbset(db, key, strlen(key), reset_value, strlen(reset_value)); + queue_add(&requests, key); + } else{ + ++hits; + } + sprintf(out, "VALUE %s 0 %d\r\n%s\r\nEND\r\n", key, (int)strlen(value), value); } kcfree(result_buffer); } else{ ++misses; - sprintf(out, "VALUE %s 0 2\r\n{}\r\nEND\r\n", key); - kcdbset(db, key, strlen(key), "0", 1); + sprintf(out, "VALUE %s 0 0\r\n\r\nEND\r\n", key); + char value[16]; + int now = (int)time(NULL); + sprintf(value, "%10d:0", now + 60); + kcdbset(db, key, strlen(key), value, strlen(value)); queue_add(&requests, key); } fputs(out, client); @@ -107,7 +124,7 @@ int handle_command(char* buffer, FILE* client){ char* command; list_shift(tokens, &command); if(command != NULL){ - if(strncmp(command, "get", 3) == 0){ + if(strcmp(command, "get") == 0){ handle_get(tokens, client); } else if(strcmp(command, "stats") == 0){ handle_stats(tokens, client); diff --git a/src/handlers.h b/src/handlers.h index 24ac82e..672fbe8 100644 --- a/src/handlers.h +++ b/src/handlers.h @@ -1,7 +1,7 @@ #ifndef FAST_CACHE_HANDLERS #define FAST_CACHE_HANDLERS #include - +#include #include "common.h" #include "queue.h" diff --git a/src/main.c b/src/main.c index 55e0d46..407a7a0 100644 --- a/src/main.c +++ b/src/main.c @@ -16,23 +16,27 @@ sig_atomic_t misses = 0; sig_atomic_t hits = 0; sig_atomic_t connections = 0; +int record_expire_time = 3600; static struct option long_options[] = { {"port", 1, 0, 0}, {"url", 1, 0, 0}, {"workers", 1, 0, 0}, {"cache", 1, 0, 0}, + {"expire", 1, 0, 0}, {"help", 0, 0, 0}, {NULL, 0, NULL, 0} }; void usage(){ const char* usage_str = - "Usage: fast-cache [-p|--port ] [-w|--workers ] [-u|--url ] [-c|--cache ] [-h|--help]\r\n" + "Usage: fast-cache [-h|--help] [-p|--port ] [-w|--workers ] [-u|--url ]\r\n" + " [-c|--cache ] [-e|--expire ]\r\n" "\t-p, --port\t- which port number to bind too [default: 7000]\r\n" "\t-w, --workers\t- how many background workers to spawn [default: 10]\r\n" "\t-u, --url\t- which url to proxy requests to [default: http://127.0.0.1:8000]\r\n" "\t-c, --cache\t- kyoto cabinet cache to use [default: \"*\"]\r\n" + "\t-e, --expire\t- the expiration time in seconds from when a record is cached [default:3600]\r\n" "\t-h, --help\t- display this message\r\n"; printf("%s", usage_str); } @@ -114,6 +118,9 @@ int main(int argc, char* argv[]){ c = 'c'; break; case 4: + c = 'e'; + break; + case 5: c = 'h'; break; default: @@ -142,12 +149,17 @@ int main(int argc, char* argv[]){ case 'c': cache_file = optarg; break; + case 'e': + record_expire_time = atoi(optarg); + if(record_expire_time <= 0){ + fprintf(stderr, "Invalid Expiration Time: %s\r\n", optarg); + exit(1); + } + break; case 'h': usage(); exit(0); case '?': - usage(); - exit(1); default: fprintf(stderr, "Unknown Option: %c\r\n", c); usage(); diff --git a/src/proxy.c b/src/proxy.c index 1526456..c8b0cff 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -38,7 +38,10 @@ void* call_proxy(void* arg){ curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEDATA, result); res = curl_easy_perform(curl); - kcdbset(db, next, strlen(next), result->data, strlen(result->data)); + char value[1024]; + int now = (int)time(NULL); + sprintf(value, "%10d:%s", now + record_expire_time, result->data); + kcdbset(db, next, strlen(next), value, strlen(value)); if(url != NULL){ free(url); diff --git a/src/proxy.h b/src/proxy.h index d0b5e2e..5c6af56 100644 --- a/src/proxy.h +++ b/src/proxy.h @@ -2,6 +2,7 @@ #define FAST_CACHE_PROXY #include +#include #include "common.h" #include "queue.h"