3

We use Gravatar on our website, but we want to let users upload their profile images directly in an effort to improve user experience similarly to what Stackexchange has been doing.

On our website users can follow each other, comment, 'like' and interact in ways that cause content to be generated directly and indirectly. This means that cache-fragments with user avatars are scattered all over the place, and we can't find a reasonable way to invalidate it without negatively affecting render performance.

Here are the possible solutions we've looked at:

Option 1) Take the Gravatar approach and set very short Expires/Cache-Control max-age headers and recycle the same image filename.

Option 2) Use a placeholder image for all users with a data attribute containing the user ids that are read by JavaScript and used to make a second Ajax request asking the server for up-to-data avatars.

Is there any better way to solve this problem? Are we overlooking something?

Community
  • 1
  • 1
Bob
  • 81
  • 1
  • 2

1 Answers1

0

I think I understand your question, but Im not sure I understand option 2 as a solution, which may indicate that its not a great solution. If it was me I would just cache the html surrounding the user gravatar, which is being reused, in a top level cache key, keyed by the users id. I.E.

<% cache("user_#{user.id}_profile_image") do %>
  <img src="blahblahblah or gravatar helper code or whatev">
<% end %>

If you're concerned about a user uploading a gravatar, and subsequently having a cached default gravatar image, I would say your best options are:

1) In the users profile area where you send them to upload a gravatar, have a refresh link which points to a refresh action which actually invalidates that users cache fragment. i.e.

expire_fragment "user_#{user.id}_profile_image"

(http://guides.rubyonrails.org/caching_with_rails.html)

2) you could instead of using the default gravatar redirect to upload an image, you could intercept the click, send it to your own controller, schedule a background task to be run in 15 minutes or so, and then render a javascript response which redirects them to the actual gravatar page to upload their pic. The background worker would then clear the fragment at a later time when it ran. This assumes they actually upload their image and is alltogether I would say a terrible idea.

Honestly though, Im not sure why you are concerned about caching the gravatar to begin with. It's hitting gravatars servers so it causes no load on your server, storing it yourself seems a bit self defeating to the point of using gravatar. Hopefully your question was simpler (and was just: how to clear default cached gravatar image when user uploads their own image), which can be solved by #1), which will allow you to expire the cached gravatar image and recache it using your own image, after the user uploads their image. (which, next time the page was rendered would recache the image because youd have some logic like:

<% cache("user_#{user.id}_profile_image") do %>
  <% if user.has_uploaded_image? %>
     <%= display_profile_image %>
  <% else %>
    <%= display_gravatar_image %>
  <% end %>
<% end %>
thrice801
  • 1,671
  • 5
  • 23
  • 31