Skip to content

De/Serialize connect.AnyResponse to be used in caching interceptors #771

Answered by sudorandom
f0o asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @f0o,

Please read all the way to the end. There's a note at the very bottom that's important.

Unless you're doing something strange, the Any() method on connect.AnyReponse should pretty much always be a proto.Message type. You just have to make a type assertion so that you can use proto.Marshal() on it to get the encoded message as a []bytes type that you can store in a cache. It would look something like this,

func NewCacheInterceptor() connect.UnaryInterceptorFunc {
	interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
		return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
			// TODO: Try to fetch the cached response an…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@f0o
Comment options

Answer selected by jhump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants