2

My application handles an html form with a POST metod, and the webapp should generate a static file (xls) for the user entered data.

I'd like to generate a static link for user i.e. /download/{uuid}. This URI should return the static generated file so that user can share or bookmark this link (such link could be destroyed after some time, may be some days).

My webapp doesn't use any db, and I'd like to avoid using db only for one table with key-values data.

The question is how to implement this approach in Spring MVC considering thread safety?

Should I create a Spring bean with singleton scope with syncronized methods for adding/reading Map of uuid/file path?

Please, tell me the best way to solve this problem.

4

2 回答 2

2

If you are going to use an in-memory data structure, then a singleton scoped object would be one approach. You could create a custom bean, or you could simply create a synchronized HashMap (by wrapping it using Collections.synchronizedMap), or a ConcurrentHashMap instance.

But the problem with that approach is twofold:

  1. It doesn't scale. If you have too many users, or the key-value data is to large, then you can end up using too much memory.

  2. The key-value data will be lost when your server is (hard) restarted.

I think you should consider a database, or alternatively considering implementing persistent sessions and storing the key-value data as session state.

于 2013-10-06T12:14:48.233 回答
0

Thinking outside the box, there is one solution that requires no storage, and yet being thread safe. Instead of creating the file and then generate the static link (and put their relation in a map, database or other key-value storage), you create the resulting link first, and then you use the link to generate the name of the file using some kind of transformation method. Next, when the user requests the same file later on you use the same transformation method to re-generate the name of the file, and thus you need no storage at all! Simplest implementation of the transformation method is of course to use the url as the file name (be aware of URL encoding / decoding), but you can make is as difficult as you want.

于 2013-10-06T16:46:07.937 回答