Preamble: it's a self-assigned and pure syntetic task to learn (and remember what I already knew) C# threads and synchronization and data structures.
The story:
Let's say I have a dictionary <string, string>
that represents a path (http) to a file by some key, ie:
foo => http://domain.tld/file1
bar => http://domain2.tld/file2
And I'd like to implement a class that will implement an interface with 2 methods:
String Rand();
String Get(String key);
The first method would pick the file randomly from all the available, and the Get
would return a particular file, or to be precise - local path to a downloaded file.
The class should be thread-safe, so that if several threads request the same key
with Get()
or the Rand()
picks the same item - then only one thread should actually download a file to a local drive, or the path should be retrieved immediately if a file has already been downloaded.
So, that's where I'm in stuck.
How would I synchronize a "downloader" so that the same file wasn't being downloaded twice?
How could I limit amount of simultaneous downloads?
PS: I'm not asking about any code, just a keywords to a data structures, classes and patterns that would be useful for this task.
PPS: the task is 100% abstract so if you think that some changes to the requirements can make it more interesting/useful for me (as a learner) - you're welcome with changes.