Skip to content

Commit 7c7d63e

Browse files
committed
refactor: replace FileCache with FileCacheEx in HttpHandler and HttpServer
1 parent f5e9a67 commit 7c7d63e

3 files changed

Lines changed: 14 additions & 14 deletions

File tree

http/server/HttpHandler.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ int HttpHandler::defaultStaticHandler() {
608608
return status_code;
609609
}
610610

611-
// FileCache
612-
FileCache::OpenParam param;
611+
// FileCacheEx
612+
FileCacheEx::OpenParam param;
613613
param.max_read = service->max_file_cache_size;
614614
param.need_read = !(req->method == HTTP_HEAD || has_range);
615615
param.path = req_path;
@@ -689,7 +689,7 @@ int HttpHandler::defaultErrorHandler() {
689689
std::string filepath = service->document_root + '/' + service->error_page;
690690
if (files) {
691691
// cache and load error page
692-
FileCache::OpenParam param;
692+
FileCacheEx::OpenParam param;
693693
fc = files->Open(filepath.c_str(), &param);
694694
}
695695
}
@@ -798,7 +798,7 @@ int HttpHandler::GetSendData(char** data, size_t* len) {
798798
}
799799
// File service
800800
if (fc) {
801-
// FileCache
801+
// FileCacheEx
802802
// NOTE: no copy filebuf, more efficient
803803
header = pResp->Dump(true, false);
804804
fc->prepend_header(header.c_str(), header.size());
@@ -842,8 +842,8 @@ int HttpHandler::GetSendData(char** data, size_t* len) {
842842
}
843843
case SEND_DONE:
844844
{
845-
// NOTE: remove file cache if > FILE_CACHE_MAX_SIZE
846-
if (fc && fc->filebuf.len > FILE_CACHE_MAX_SIZE) {
845+
// NOTE: remove file cache if > max_file_size
846+
if (fc && fc->filebuf.len > files->GetMaxFileSize()) {
847847
files->Close(fc->filepath.c_str());
848848
}
849849
fc = NULL;

http/server/HttpHandler.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "HttpService.h"
55
#include "HttpParser.h"
6-
#include "FileCache.h"
6+
#include "FileCacheEx.h"
77

88
#include "WebSocketServer.h"
99
#include "WebSocketParser.h"
@@ -71,8 +71,8 @@ class HttpHandler {
7171
uint64_t last_recv_pong_time;
7272

7373
// for sendfile
74-
FileCache *files;
75-
file_cache_ptr fc; // cache small file
74+
FileCacheEx *files;
75+
file_cache_ex_ptr fc; // cache small file
7676
struct LargeFile : public HFile {
7777
HBuf buf;
7878
uint64_t timer;

http/server/HttpServer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct HttpServerPrivdata {
1919
std::vector<hthread_t> threads;
2020
std::mutex mutex_;
2121
std::shared_ptr<HttpService> service;
22-
FileCache filecache;
22+
FileCacheEx filecache;
2323
};
2424

2525
static void on_recv(hio_t* io, void* buf, int readbytes) {
@@ -86,7 +86,7 @@ static void on_accept(hio_t* io) {
8686
handler->service = service;
8787
// websocket service
8888
handler->ws_service = server->ws;
89-
// FileCache
89+
// FileCacheEx
9090
HttpServerPrivdata* privdata = (HttpServerPrivdata*)server->privdata;
9191
handler->files = &privdata->filecache;
9292
hevent_set_userdata(io, handler);
@@ -132,14 +132,14 @@ static void loop_thread(void* userdata) {
132132
service->Static("/", service->document_root.c_str());
133133
}
134134

135-
// FileCache
136-
FileCache* filecache = &privdata->filecache;
135+
// FileCacheEx
136+
FileCacheEx* filecache = &privdata->filecache;
137137
filecache->stat_interval = service->file_cache_stat_interval;
138138
filecache->expired_time = service->file_cache_expired_time;
139139
if (filecache->expired_time > 0) {
140140
// NOTE: add timer to remove expired file cache
141141
htimer_t* timer = htimer_add(hloop, [](htimer_t* timer) {
142-
FileCache* filecache = (FileCache*)hevent_userdata(timer);
142+
FileCacheEx* filecache = (FileCacheEx*)hevent_userdata(timer);
143143
filecache->RemoveExpiredFileCache();
144144
}, filecache->expired_time * 1000);
145145
hevent_set_userdata(timer, filecache);

0 commit comments

Comments
 (0)