英文:
golang singletons in beego
问题
我正在尝试使用Golang和Beego。我来自Java/Spring的背景,我在实现一个Web应用程序时遇到了一些困难。我有一个控制器(我希望它是一个单例),控制器中还有一个服务(我也希望它是一个单例)。我以为如果我将我的服务定义为指针,那么我将始终使用相同的地址(一个单例)。但事实证明并非如此。
我的路由看起来像这样:
beego.Router("/", &controllers.SessionController{}, "get:Login")
我的SessionController看起来像这样:
type SessionController struct {
baseController
userService *services.UserService
}
func (this *SessionController) Prepare() {
this.baseController.Prepare()
if this.userService == nil {
beego.Info("user service was nil")
this.userService = factories.NewUserService()
}
}
我的日志始终显示每个请求时用户服务为nil。我该如何获得一个控制器的单个实例,并且该实例只初始化一次我的用户服务?
英文:
I'm trying out both Golang and Beego. I come from a Java/Spring background and I'm having a slightly difficult time implementing a singleton pattern for a webapp I'm developing. I have a controller (which I want to be a singleton) and I have a service within my controller (which I also want to be a singleton). I thought if I made my service a pointer, then I'd always use the same address (a singleton). This is not proving true.
My route looks like this<br/>
beego.Router("/", &controllers.SessionController{}, "get:Login")
My SessionController looks like this <br/>
type SessionController struct {
baseController
userService *services.UserService
}
func (this *SessionController) Prepare() {
this.baseController.Prepare()
if this.userService == nil {
beego.Info("user service was nil")
this.userService = factories.NewUserService()
}
}
My logs always show that the user service is nil upon each request. How can I get a single instance of my controller with a single (initialized only once) instance of my user service?
答案1
得分: 2
将用户服务设置为单例:
var globalUserService = factories.NewUserService()
type SessionController struct {
baseController
userService *services.UserService
}
func (this *SessionController) Prepare() {
this.baseController.Prepare()
if this.userService == nil {
beego.Info("用户服务为空")
this.userService = globalUserService
}
}
注意:以上代码是Go语言代码,用于将用户服务设置为单例。在SessionController
的Prepare
方法中,首先检查userService
是否为空,如果为空,则将其设置为全局的globalUserService
。
英文:
Make the user Service a singleton:
var globalUserService = factories.NewUserService()
type SessionController struct {
baseController
userService *services.UserService
}
func (this *SessionController) Prepare() {
this.baseController.Prepare()
if this.userService == nil {
beego.Info("user service was nil")
this.userService = globalUserService
}
}
答案2
得分: 2
Beego框架的其中一位创建者告诉我,该框架每个请求都会创建一个新的控制器实例,所以不太可能创建一个单例
英文:
1 of the creators of the Beego framework has mentioned to me that the framework creates a new instance of a controller per request, so it's not really possible to create a singleton
答案3
得分: 0
以下是关于在Golang中创建单例模式的一切你需要知道的内容:
var instance *singleton
func GetInstance() *singleton {
if instance == nil {
instance = &singleton{} // <--- NOT THREAD SAFE
}
return instance
}
但这不是线程安全的,你需要使用Sync包中的Once函数。
示例:-
import (
"sync"
)
type singleton struct {
}
var instance *singleton
var once sync.Once
func GetInstance() *singleton {
once.Do(func() {
instance = &singleton{}
})
return instance
}
英文:
Here is everything you need to know about creating singleton in Golang
var instance *singleton
func GetInstance() *singleton {
if instance == nil {
instance = &singleton{} // <--- NOT THREAD SAFE
}
return instance
}
But that is not THREAD SAFE , you will need to use the Sync once
Example :-
import (
"sync"
)
type singleton struct {
}
var instance *singleton
var once sync.Once
func GetInstance() *singleton {
once.Do(func() {
instance = &singleton{}
})
return instance
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论