Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Same cache, multiple addresses #115

Open
neurosnap opened this issue Dec 6, 2024 · 3 comments
Open

Same cache, multiple addresses #115

neurosnap opened this issue Dec 6, 2024 · 3 comments

Comments

@neurosnap
Copy link

Greetings! Thanks for the awesome http cache plugin for Caddy, it's overall working great!

We have a Caddyfile with multiple addresses and want them to share the same Otter cache and were wondering if that is possible with this project? If not, do you have any recommendations for an alternative storage system to handle this use case?

Basically we host sites as subdomains on our root domain (e.g. https://erock-neovimcraft.pgs.sh) as well as allowing users to bring their own custom domains (e.g. https://neovimcraft.com) using on_demand tls. As a result, we want both sites to share the same cache which we have labeled with a surrogate-key (e.g. erock-neovimcraft).

Let me know if you need more information, thanks again!

{
	on_demand_tls {
		ask http://0.0.0.0:3000/check
		interval 1m
		burst 10
	}
	servers {
		metrics
		trusted_proxies static 0.0.0.0/0
	}
	cache {
		ttl 300s
		max_cacheable_body_bytes 1000000
		otter
		api {
			souin
		}
	}
}

(httpcache) {
	cache {
		regex {
			exclude /check
		}
	}
}

# custom domains
:443 {
        import httpcache

	reverse_proxy 0.0.0.0:3000
	encode zstd gzip

	tls internal {
		on_demand
	}
}

# subdomains
*.{$APP_DOMAIN} {
        import httpcache

	reverse_proxy 0.0.0.0:3000
	encode zstd gzip

        tls internal
}

# root site
{$APP_DOMAIN} {
	reverse_proxy 0.0.0.0:3000
	encode zstd gzip

        tls internal

	route {
		@souinApi path /souin-api/*
		basic_auth @souinApi {
			cacheuser $2a$14$NuCZqIy/NGKEmK89bNSKseBidpwPK4HiD4rqgnLftUHpWVAfV4xGO
		}
		cache {
			regex {
				exclude /check
			}
		}
		reverse_proxy 0.0.0.0:3000
	}
}
@darkweak
Copy link
Collaborator

darkweak commented Dec 8, 2024

Hey @neurosnap if you want to share the cache for two different domains, you can use the key.disable_host directive or use your own template with key.template using caddy placeholders.

# Your configuration...
:443 {
    route {
        cache {
            key {
                disable_host
            }
        }
    }
}

# Or like this...
:443 {
    route {
        cache {
            key {
                template {http.request.method}-{http.request.path}
            }
        }
    }
}

Tell me if that's what you want.

@mac-chaffee
Copy link

Re-posting my message from the PR:

Yeah I think more accurately, we don't want to share the cache between two different domains. We want to share the cache between two different caddy site blocks, which I'm 99% sure is not possible based on my research here: #106 (comment)

I see that we are using caddy.UsagePool (docs) in some places to ensure there's only one instance of some things, so maybe there's a bug that is keeping the caches in different site blocks separate.

@mac-chaffee
Copy link

mac-chaffee commented Dec 10, 2024

Ahh yep I've found that the behavior I noticed where the two different site blocks do not share a cache may only be present in Otter. The behavior does not happen with the default Souin storage provider or Badger.

{
  debug
  log {
    level debug
  }
  cache {
    ttl 300s
    max_cacheable_body_bytes 1000000
    badger
    api {
      souin
    }
  }
}

http://localhost:8080 {
  respond "Hello from port 8080"
  cache
}

http://localhost:8081 {
  respond "Hello from port 8081"
  cache
}
$ curl localhost:8080
Hello from port 8080

$ curl localhost:8081
Hello from port 8081

$ curl -u testuser:password localhost:8080/souin-api/souin/
["GET-http-localhost:8081-/","GET-http-localhost:8080-/"]

In the debug logs, I can see that including cache in two different site blocks does result in Start() and Initialize() being called twice, but when using Badger or the default provider, the Storer struct is pointing to the same address both times:

2024/12/10 00:20:41.150	DEBUG	http.handlers.cache	You're running Souin with the following storages in this order BADGER
2024/12/10 00:20:41.150	DEBUG	http.handlers.cache	Storer initialized: []types.Storer{(*badger.Badger)(0x14000294420)}.
...
2024/12/10 00:20:41.151	DEBUG	http.handlers.cache	You're running Souin with the following storages in this order BADGER
2024/12/10 00:20:41.151	DEBUG	http.handlers.cache	Storer initialized: []types.Storer{(*badger.Badger)(0x14000294420)}.

When using Otter, those addresses are different:

2024/12/10 00:28:26.276	DEBUG	http.handlers.cache	You're running Souin with the following storages in this order OTTER
2024/12/10 00:28:26.276	DEBUG	http.handlers.cache	Storer initialized: []types.Storer{(*otter.Otter)(0x14000095940)}.
...
2024/12/10 00:28:26.276	DEBUG	http.handlers.cache	You're running Souin with the following storages in this order OTTER
2024/12/10 00:28:26.276	DEBUG	http.handlers.cache	Storer initialized: []types.Storer{(*otter.Otter)(0x140000954a0)}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants