-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
How to use cache in MVC #850
Comments
Thank you @Zeno-Code If you want to use it as middleware for the entire controller I'll show you 4 different methods for adding a middleware into an mvc application, // 1
mvc.Configure(app.Party("/user"), func(m *mvc.Application) {
m.Router.Use(cache.Handler(10*time.Second))
}) // 2
// same:
userRouter := app.Party("/user")
userRouter.Use(cache.Handler(10*time.Second))
mvc.Configure(userRouter, ...) // 3
// same:
userRouter := app.Party("/user", cache.Handler(10*time.Second))
mvc.Configure(userRouter, ...) // 4
// same:
app.PartyFunc("/user", func(r iris.Party){
r.Use(cache.Handler(10*time.Second))
mvc.Configure(r, ...)
}) If you want to use a middleware for a single route, var myMiddleware := myMiddleware.New(...) // this should return an iris/context.Handler
type UserController struct{}
func (c *UserController) GetSomething(ctx iris.Context) {
// ctx.Proceed checks if myMiddleware called `ctx.Next()`
// inside it and returns true if so, otherwise false.
nextCalled := ctx.Proceed(myMiddleware)
if !nextCalled {
return
}
// else do the job here, it's allowed
} And last, if you want to add a middleware on a specific method package main
import (
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/cache"
"github.com/kataras/iris/mvc"
)
var cacheHandler = cache.Handler(10 * time.Second)
func main() {
app := iris.New()
// You don't have to use .Configure if you do it all in the main func
// mvc.Configure and mvc.New(...).Configure() are just helpers to split
// your code better, here we use the simplest form:
m := mvc.New(app)
m.Handle(&exampleController{})
app.Run(iris.Addr(":8080"))
}
type exampleController struct{}
func (c *exampleController) AfterActivation(a mvc.AfterActivation) {
// select the route based on the method name you want to
// modify.
index := a.GetRoute("Get")
// just prepend the handler(s) as middleware(s) you want to use.
// or append for "done" handlers.
index.Handlers = append([]iris.Handler{cacheHandler}, index.Handlers...)
}
func (c *exampleController) Get() string {
// refresh every 10 seconds and you will see different time output.
now := time.Now().Format("Mon, Jan 02 2006 15:04:05")
return "last time executed without cache: " + now
} |
Thanks Reply. If use Use the |
It returns the same content because of the cache implementation, it depends on the handler not the path itself, I will update it to be used more generally and not per-handler only and after that you will use the // 4 method and it will work like a charm, I show you the advanced |
@Zeno-Code I prepared an example, although it's simple but many may have this question because the previous implementation didn't have those features, here you're: https://github.com/kataras/iris/blob/master/_examples/mvc/middleware/main.go var cacheHandler = cache.Handler(10 * time.Second)
func main() {
app := iris.New()
mvc.Configure(app, configure)
app.Run(iris.Addr(":8080"))
}
func configure(m *mvc.Application) {
m.Router.Use(cacheHandler)
m.Handle(&exampleController{
timeFormat: "Mon, Jan 02 2006 15:04:05",
})
}
// [...] Don't forget to upgrade using |
It worked. Very perfect, Thanks |
First congratulations 10.0 release
I want to use "github.com/kataras/iris/cache" on MVC Methods
How to use it?
The text was updated successfully, but these errors were encountered: