cache-handler icon indicating copy to clipboard operation
cache-handler copied to clipboard

How to remove listening port (5443) and protocol scheme (http/https) from the cache_key when it is not regular port while preserving the hostname?

Open Zerorigin opened this issue 10 months ago • 6 comments

# Caddyfile config block

{
	admin off
	order cache before rewrite
	cache {
		mode bypass
		allowed_http_verbs GET HEAD
		stale 1h
		key {
			disable_body
			disable_method
			disable_query
		}
	}
}

(spec_cache) {
	@ArchivesCachePolicy {
		not header_regexp Cookie .*(?:auth|Auth|sess|SESS|token|Token|user|User).*
		not path /*sitemap.xml */admin* */login* */auth* */sync*

		# spec files ext to match
		path_regexp .+\.(?:7z|rar|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx|wps|et|dps)\??.*
	}
	cache @ArchivesCachePolicy {
		stale 1h
		ttl 1h
		# Archive or Others
		cache_keys {
			.+\\.(?:7z|rar|zip)\\??.* {
				disable_body
				disable_method
				disable_query
			}
		}
		# Docs
		cache_keys {
			.+\\.(?:pdf|txt|doc|docx|xls|xlsx|ppt|pptx|wps|et|dps)\\??.* {
				disable_body
				disable_method
				disable_query
			}
		}
	}


	@SrcCodesCachePolicy {
		not header_regexp Cookie .*(?:auth|Auth|sess|SESS|token|Token|user|User).*
		not path /*sitemap.xml */admin* */login* */auth* */sync*

		# spec files ext to match
		path_regexp .+\.(?:css|js)\??.*
	}
	cache @SrcCodesCachePolicy {
		stale 3h
		ttl 240h
		# HTML Source Codes
		cache_keys {
			.+\\.(?:css|js)\\??.* {
				disable_body
				disable_method
				disable_query
			}
		}
	}
}

example.com,
https://example.com:5443 {
	import spec_cache
	handle_path /specific.txt {
		respond "Hello, World!"
		header "Content-Type" "text/plain"
	}
	tls /path/to/ca.crt /path/to/ca.key
}

 

# Test results
# curl -Iks -X GET 'https://example.com:5443/specific.txt' | grep Souin
Cache-Status: Souin; hit; ttl=2909; key=https-example.com:5443-/specific.txt

# curl -Iks -X GET 'https://example.com/specific.txt' | grep Souin
Cache-Status: Souin; hit; ttl=2900; key=https-example.com-/specific.txt

# curl -Is -X GET 'http://example.com/specific.txt' | grep Souin
Cache-Status: Souin; hit; ttl=2890; key=http-example.com-/specific.txt

  BTW, it's too complicated to set different stale and ttl timeout value for different files.

Zerorigin avatar Apr 05 '24 10:04 Zerorigin

My idea is to add disable_proto and disable_port to the cache_keys block and move ttl and stale into the cache_keys to configure them.

This simplifies cache configuration for different static file types while improving cache efficiency.

Zerorigin avatar Apr 09 '24 23:04 Zerorigin

Additionally, the subcommand hide just hides the key value and does not remove the headers (Age and Cache-Status), which is slightly different than expected, and should perhaps be broken down into hide key, hide header, and hide off, which would make it easier for users who want to remove the headers.

Zerorigin avatar Apr 09 '24 23:04 Zerorigin

Hello @Zerorigin, I can add more directive to enable/disable the key. Btw I could add a TTL and stale directive to the cache_key block to be able to override the values. What do you think?

darkweak avatar Apr 12 '24 12:04 darkweak

Hello @Zerorigin, I can add more directive to enable/disable the key. Btw I could add a TTL and stale directive to the cache_key block to be able to override the values. What do you think?

This is great, just what I need.

Zerorigin avatar Apr 15 '24 04:04 Zerorigin

@Zerorigin in the next release you'll be able to create your own key template. e.g.

key {
    template "{host}-something-constant-{method} other {query}"
}

darkweak avatar May 12 '24 19:05 darkweak

And I still have to implement the TTL and stale directly in the cache_keys.

darkweak avatar May 12 '24 19:05 darkweak

@Zerorigin in the next release you'll be able to create your own key template. e.g.

key {
    template "{host}-something-constant-{method} other {query}"
}

The rest isn't urgent. 😄 Thanks for your hard work. ❤️

Zerorigin avatar May 13 '24 14:05 Zerorigin