nicksawhney / bernie-sits

A site that went kinda viral that lets you put Bernie Sanders in places
GNU Affero General Public License v3.0
307 stars 50 forks source link

Caching #24

Open nicksawhney opened 3 years ago

nicksawhney commented 3 years ago

The biggest cost for this site was API requests. Caching will help offset this greatly. It would be nice to cache from some kind of unique image identifier instead of input string, since different inputs can cause the same image to be returned.

keevie commented 3 years ago

looks like we can request metadata for free, and then maybe cache on lat/lng? https://developers.google.com/maps/documentation/streetview/metadata

nicksawhney commented 3 years ago

@keevie Do you wanna go ahead and implement this in a PR? I can assign it to you if you'd like

nicksawhney commented 3 years ago

Also this is great, goes nicely with the automated heroku deploy we've got going in the README https://github.com/nicksawhney/bernie-sits/discussions/22#discussioncomment-306946

samarmohan commented 3 years ago

What are the costs for this site????

nicksawhney commented 3 years ago

hosting and api cost. Let's keep this discussion to how we want to implement caching.

keevie commented 3 years ago

Sure, I can take a crack at it -- I think I'm about done for the day though, so I might not get to it for a few days at least.

This is also going to be more dependent on where it's running than other things -- if you want to keep it tied to heroku that could make sense to me! That said I tried for a few minutes to get it running using that magic button and got a mysterious error, so I think it might need a little tweaking.

As for implementation details after thinking about it for a minute I think something dead simple with redis makes sense. We can turn persistence on and use pano_id as the keys, cache the images, and then just set whatever max size makes sense and an eviction policy of LRU or LFU.

The app code should be as simple as

pano_id = get_pano_id_from_google(location_string)

if redis.get(pano_id):
  return redis.get(pano_id)
else:
  image = get_image_from_google()
  redis.set(pano_id, image)
  return image
nicksawhney commented 3 years ago

Looks great. Keeping it tied to heroku for now seems like a solid idea -- more deployment options can be added over time (a great first issue for interested contributors). if you're working on this in a fork I'd be happy to contribute there as well. I'll figure out the magic button errors.