In-memory Cache
If the Application requires the files to be located physically in the same machine that the server runs but you want to cache the files in memory before serve to achieve the maximum performance then the DirOptions.Cache is the field you have to enable.
The DirOptions.Cache accepts DirCacheOptions:
1
type DirCacheOptions struct {
2
// Enable or disable cache.
3
Enable bool
4
// Minimum content size for compression in bytes.
5
CompressMinSize int64
6
// Ignore compress files that match this pattern.
7
CompressIgnore *regexp.Regexp
8
// The available sever's encodings to be
9
// negotiated with the client's needs,
10
// common values: gzip, br.
11
Encodings []string
12
13
// If greater than zero then prints
14
// information about cached files to the stdout.
15
// If it's 1 then it prints
16
// only the total cached and after-compression
17
// reduced file sizes.
18
// If it's 2 then it prints it per file too.
19
Verbose uint8
20
}
Copied!
The DirCacheOptions allows to cache files based on the available compressions(!) too. So if a client requests a gzip version of a file, then the server will be serve the gzip data directly, without the necessity to encode it on fly. This can improve your site's speed to over 50%. This is done automatically by the framework, you just have to provide available Encodings that you want to support and Iris can handle the rest work.
Usage
1
app.HandleDir("/public", iris.Dir("./assets"), iris.DirOptions{
2
IndexName: "index.html",
3
Cache: iris.DirCacheOptions{
4
Enable: true,
5
Encodings: []string{"gzip"},
6
CompressIgnore: iris.MatchImagesAssets,
7
CompressMinSize: 30 * iris.B, // 30 bytes.
8
},
9
})
Copied!
The iris.MatchImagesAssets is just a regular expression which ignores to compress all pre-compressed images for a second time, e.g. .jpg.
Last modified 1yr ago
Export as PDF
Copy link