Iris
Getting Started
  • What is Iris
  • 📌Getting started
    • Installation
    • Quick start
  • 🔌Routing
    • Middleware
    • API Versioning
  • 🗜️Compression
    • Index
  • ✈️Redirect
    • Redirect from Context
    • Rewrite Middleware
    • Multi Application Instances
  • 🖼️ View/Templates
    • Documentation
    • Benchmarks
    • ➲ Examples
  • 📁File Server
    • Introduction
    • Listing
    • In-memory Cache
    • HTTP/2 Push + Embedded + Cache and Compression
    • The PrefixDir function
    • Serve files from Context
    • ➲ Examples
  • 🌎Localization
    • Documentation
    • Sitemap
    • ➲ Examples
  • 🛡️Security
    • Basic Authentication
    • CORS
    • Sessions & Cookies
    • CSRF
    • JSON Web Tokens
    • Access Control
    • Anti-bot CAPTCHA
    • ➲ Examples
  • 🚀Responses
    • Text
    • HTML
    • Markdown
    • XML
    • YAML
    • Binary
    • JSON
    • JSONP
    • Problem
    • Protocol Buffers
    • MessagePack
    • Gzip
    • Content Negotiation
    • Stream
    • Server-Sent Events
    • HTTP/2 Push
    • Recorder
    • Outroduction
    • ➲ Examples
  • 📥Requests
    • URL Query
    • Headers
    • URL Path Parameters
    • Form
    • Text
    • XML
    • YAML
    • Binary
    • JSON
    • Validation
    • Protocol Buffers
    • MessagePack
    • Gzip
    • ➲ Examples
  • 💉Dependency Injection
    • Documentation
    • Register Dependency from Context
    • Inputs
    • Outputs
    • ➲ Examples
  • 🦏MVC
    • Quick start
    • Documentation
    • Handle Errors
    • Sessions
    • Websockets
    • gRPC
    • ➲ Examples
  • 🤓Resources
    • Examples
    • Starter Kits
    • Publications
    • Benchmarks
    • Support
  • 📘Contents
    • Host
      • Automatic Public Domain with TLS
    • Configuration
    • Routing
      • Path Parameter Types
      • Reverse Lookups
      • Handle HTTP errors
      • Subdomains
      • Wrap the Router
      • Context Methods
    • HTTP Method Override
    • HTTP Referrer
    • URL Query Parameters
    • Forms
    • Model Validation
    • Cache
    • Cookies
    • Sessions
      • Database
      • Flash Messages
    • Websockets
    • Sitemap
    • Localization
    • Testing
Powered by GitBook
On this page

Was this helpful?

  1. 📁File Server

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.

PreviousListingNextHTTP/2 Push + Embedded + Cache and Compression

Last updated 2 years ago

Was this helpful?