Pergunta

I know that by default, the scope of service in Grails is singleton. Which means one instance gets created and passed around. When dealing with many users, such as Facebook or Twitter, should the scope be session or something else ? I appreciate some explanation. I'm designing a social network kind of website with Grails, I'm wondering what to set the scope to. I know that one way to figure out is to assess if using one single instance is gonna cause issue, but again I don't know so much about it.

Foi útil?

Solução

Having singleton scope of services in Grails is only a problem if you make the service stateful, meaning you declare instance variables and store things related to particular requests in them, in which case those instance variables are available to all the requests that are using that service. The appropriate place to store user-specific data on the server is in the HTTP Session, not in instance variables in controllers or services.

Outras dicas

Storing user specific, per-session data in a session scoped service will work, and is common in small web apps. It's more or less equivalent to storing the data directly in the HTTP session, but saves you from having to deal with the session object directly.

However, stateful services are generally disliked for larger apps. You may need to reuse your service in contexts without a session, e.g. as an API. Additionally, depending on the type of data, the session might not be the best choice; cookies on the client side, persisted domain objects, or a name/value store like memcached might make more sense. It depends on the scale of the application and the type of data you're storing, and may change over your app's lifetime. Building the state into your service makes it hard to change your strategy.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top