Skip to content

Adding caching to ServeFile and ServeDir #557

@12bitentropy

Description

@12bitentropy

Feature Request

ServeFile does not use an in memory cache for "hot" files like css and js. Serving static files would be considerably slower for users that has to run their website on hard disks.

Motivation

I need to reduce the bottleneck of my slow aging hardisk from choking the performance of my app

Proposal

Implement a Read-Through cache using cloudflare's pingora crate and store a radix trie or balanced tree for precompressed files. Caching of the directory structure is useful to prevent constant checks for the file.

The balanced tree is used to determine if the precompressed files can be served, which is cheaper than checking the disk. It should not be that clients have to get the uncompressed file simply because the brotli compressed file could not be found while the other files are compressed. It might happen when files are being compressed on the go and some files have not been compressed.

There are also cases where brotli may not have the best compression ratio which makes serving files based off the smallest file size is better.

Caching invalidation is done when the time to live for the cache has expired. The filesystem tree is updated when a file cache is invalidated and file updates are checked to update the internal tree. It can use notify to check.

Some issue might appear where users are served outdated cache objects. The developer can change this using the ttl and not_found_ttl options, or their custom lookup function. Defaults to cloudflare's edge ttl defaults

Memory may be a issue to developers. The Read-Through cache can be configured based on the developer's choices to hold their choice of bytes before the cache has to evict files.

The proposal also introduces ETags as hashing is done to confirm the file hash not changed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions