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:

type DirCacheOptions struct {
	// Enable or disable cache.
	Enable bool
	// Minimum content size for compression in bytes.
	CompressMinSize int64
	// Ignore compress files that match this pattern.
	CompressIgnore *regexp.Regexp
    // The available sever's encodings to be
    // negotiated with the client's needs,
	// common values: gzip, br.
	Encodings []string

    // If greater than zero then prints
    // information about cached files to the stdout.
    // If it's 1 then it prints
    // only the total cached and after-compression
    // reduced file sizes.
	// If it's 2 then it prints it per file too.
	Verbose uint8
}

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

app.HandleDir("/public", iris.Dir("./assets"), iris.DirOptions{
    IndexName: "index.html",
    Cache: iris.DirCacheOptions{
        Enable:          true,
        Encodings:       []string{"gzip"},
        CompressIgnore:  iris.MatchImagesAssets,
        CompressMinSize: 30 * iris.B, // 30 bytes.
    },
})

The iris.MatchImagesAssets is just a regular expression which ignores to compress all pre-compressed images for a second time, e.g. .jpg.

Last updated