Our restful service has the concept of a client access token, and a user access token. If a virtual user wants to log in as a particular client, there is a process for getting that token. Once I have it, I would like to cache it and just re-use it for any subsequent requests to “log in” as that client, because that is what our service expects.
In theory, all I need to do is store a Map where the key is the unique elements (client ID or username), and the value is the token. If the map key does not exist, do the normal token getting process, then store the token in the map.
To implement the theory requires some scala knowledge that is a bit above beginner-level. In other words, a little over my level of experience.
I will need to create a tokenCache variable to store the map.
When I need to test for the existence of a key, first I will need to lock the whole map.
Once I determine that a key does NOT exist, (while the map is locked) I create that key, then lock that key, then unlock the map, get the token, store it as the key/value, then unlock that key.
Or, if I determine that the key does exist, I unlock the map, then lock the key (otherwise, I may create massive bottleneck for other users if I just happen to try to lock the key while the token fetching process is in progress), and once the lock is obtained, return the token
Does my logic look right to you?
And which scala constructs would you recommend for something like this? Some good things to google for would be greatly appreciated, since I’m sure that a simple sample class might be asking too much. But if you happen to know of some sample code that implements this kind of functionality, I’d love the pointer to it.
Thanks in advance!