redis-in-action icon indicating copy to clipboard operation
redis-in-action copied to clipboard

Chapter02 Golang CacheRequest bug

Open gopherhiro opened this issue 2 years ago • 0 comments

Here it should not be possible to cache and then call the callback function directly, so the correct way to write it would be:

if !r.CanCache(request)

func (r *Client) CacheRequest(request string, callback func(string) string) string {
	if !r.CanCache(request) {
		return callback(request)
	}

	pageKey := "cache:" + hashRequest(request)
	content := r.Conn.Get(pageKey).Val()

	if content == "" {
		content = callback(request)
		r.Conn.Set(pageKey, content, 300*time.Second)
	}
	return content
}

gopherhiro avatar Nov 20 '23 12:11 gopherhiro