I am trying to build an app where I download a list of images from different URLs into a collection view. All these images are stored inside a cache with max age and cache limit. I am storing the URL as the key for each image so that I can check if the image already exists when downloading it again and if yes, check the max age, but I have been told that storing URLs as ID is bad practice. Any suggestions on how I could save these images and retrieve their details when needed?
Asked
Active
Viewed 239 times
4
-
1It's interesting to know why isn't it a good practice? Then we can think on good solutions – Yitzchak Dec 03 '17 at 17:47
-
@Yitzchak I have been told by the HR as this is a Technical test for a position I have applied recently. I don't have more information on why it is bad practice. – Nelly v Dec 03 '17 at 17:49
-
Try to use hashing of the url. I believe it's better, it's widely used to set a hash as a key – Yitzchak Dec 03 '17 at 17:52
-
2@Yitzchak the URL doesn't necessarily represent the image itself and hence it shouldn't be stored as the unique identifier for an image. Even without the URL changing, the image itself stored under the URL can easily change (just imagine if you are storing profile images from a social network, the URL to the current profile image could easily be fixed). A better approach would be to store both the URL and the size of the image, since it is highly unlikely that if the image changes, its size will be exactly the same in bytes. – Dávid Pásztor Dec 03 '17 at 18:14
-
@DávidPásztor nice, thanks – Yitzchak Dec 03 '17 at 18:22
-
If you are consuming those images, you can’t rely on the fact that an URL will always return the same content. If you are providing the images, you should definitely make sure that for as many resources as possible (including images, css, scripts...), a given URL always returns the same content, so you can indeed apply aggressive caching strategies. If a user changed his/her profile picture, it should get a new URL, and you should have a link of redirection to that URL. – jcaron Dec 03 '17 at 19:08
-
Guys, I appreciate your response. It makes sense but the requirement is to download images in cache and on each retrieval check the max age of the content and re-download if needed. For each URL in the array I try to download, I check whether the id (which is the url) was previously saved in the cache, if it does, check the max age, get the image data and update the UI accordingly. I also need to remove the least used image if the cache reaches max limit, in which I keep the retrieval count for. I was told saving the URL as an identifier is a bad practice, so I was looking for alternative way. – Nelly v Dec 03 '17 at 19:54
1 Answers
-2
So to be clear were thinking a good way to store info about an image is instead of having an object like this :
"some_url_as_the_key":{ // details about the image }
we could have.
"number_of_bytes_of_the_image_as_key":{ //detail about the image }
Im trying to do the same and need to store the meta data bout each image in a dictionary. Which of the above would be best. I can see an issue in as much as two images could theoretically have the same number of bytes, though 1) this is unlikely, and 2) would possibly be a faster look up when searching for the meta data about an image that i need to store. I.e. a key of '12312423' (number of bytes), vs a long url like "https://www.lindofinasdoifnosidnf.sdfsdfnsdf/dfsdf' would be faster i guess.
Thoughts?

user2712678
- 17
- 1
- 2