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

ginCache #3141

Open
jiayoukun opened this issue May 5, 2022 · 1 comment
Open

ginCache #3141

jiayoukun opened this issue May 5, 2022 · 1 comment

Comments

@jiayoukun
Copy link

  • With issues:
    • Use the search tool before opening a new issue.
    • Please provide source code and commit sha if you found a bug.
    • Review existing issues and provide feedback or react to them.

Description

I read the source code and did not find the existence of cache. I wonder why there is no cache. Is it because the Web framework does not need cache? Similar to CPU L1 cache, memory TLB. Highly hit interface access cache is fast, no need to re-enter the dictionary tree traversal.
Therefore, I have two ideas, one is to manually increase the cache, the other is to count the number of interfaces, given the interval to automatically update the cache.
The code below is a demo of the first idea, and I'll fill it in if caching works.
Hope to get your reply, thank you

How to reproduce

// gin.go
type Engine struct {
	...
	cachegin bool
	cachetable map[string]bool
	...

}

func (engine *Engine)SetCache(rpath string)  {
	if engine.cachegin {
		if _, ok := engine.cachetable[rpath]; !ok {
			engine.cachetable[rpath] = true
		}else{
			panic("The cache already exists")
		}
	}
}

func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	c := engine.pool.Get().(*Context)
	...
	if rPath := c.Request.URL.Path; _, ok := engine.cachetable[rPath]; ok {
		Do(c)
	}else{
		engine.handleHTTPRequest(c) 
	}
	engine.pool.Put(c)  
}

Expectations

$ curl http://localhost:8201/hello/world
Hello world

Actual result

$ curl -i http://localhost:8201/hello/world
<YOUR RESULT>

Environment

  • go version:
  • gin version (or commit ref):
  • operating system:
@bbdshow
Copy link

bbdshow commented May 20, 2022

if you want to use cache, middleware cache plugin are a good choice for you
eg: https://github.com/gin-contrib/cache

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

2 participants