简体   繁体   中英

Best way to keep a list in a wcf service memory

I have a web application with a login system which uses a wcf service. The user type his username/password to login, then we create a unique token for the user and then the token is saved in the browser cookies to keep the user online. Everytime the user send a request, his token is sent along with the request. We keep all the tokens in a basic List in the service memory. Now, I'm asking myself if it is the best way to do this. Will iis recycle my app memory? I have read this question about keeping a list in a wcf service and it looks like I should avoid using a list in memory. What is the best alternative to this?

Thanks

Yes, if the App Pool housing your WCF Service is recycled, you will lose everything you've cached in memory, so you will also need a persistence fallback (eg RDBMS or NoSql database) which will be re-loaded on resumption.

You don't say what you want stored along with the token. If its just the string token, then a HashSet<string> might be best, and if there is other data, then a ConcurrentDictionary<string, Whatever> would work best.

Assuming that as users login / logout of your service that the token collection will need to be mutated, which means that concurrency issues need to be considered, (you've got that with the ConcurrentDictionary )

And you'll also need to consider the effects of stale tokens - you'll want to remove / delete unused tokens (memory and database).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM