Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add expirable Rails.cache capability #112

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

pinnymz
Copy link

@pinnymz pinnymz commented Feb 25, 2019

Adds the following 2 settings:

  • Shortener.cache_urls (boolean, default: false)
  • Shortener.cache_expiration (numeric, default: nil)

@pinnymz
Copy link
Author

pinnymz commented Feb 28, 2019

@craigmcnamara @jpmcgrath any thoughts on this?

@craigmcnamara
Copy link
Collaborator

Personally, I think this is better left as an application level concern.

@pinnymz
Copy link
Author

pinnymz commented Apr 3, 2019

@craigmcnamara I see your point, but the README does list this as a future improvement :)

@craigmcnamara
Copy link
Collaborator

Oh, sorry about that, @jpmcgrath probably wrote that, I'll leave this one up to him.

@stephenedwards
Copy link
Contributor

Personally, I think this is better left as an application level concern.

Since this feature is disabled by default, I definitely think it should be added. Large url tables or high volume user's will definitely benefit from fewer db hits.

@@ -125,6 +126,19 @@ def to_param

private

def self.lookup_by_token(token, additional_params, track)
shortened_url = ::Shortener::ShortenedUrl.unexpired.where(unique_key: token).first

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::Shortener::ShortenedUrl.unexpired.where(unique_key: token).first should be ::Shortener::ShortenedUrl.unexpired.find_by(unique_key: token)

Copy link
Author

@pinnymz pinnymz Sep 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lifeiscontent, pretty sure find_by is not supported by Rails 3. I'm fine with that personally, but the README states that Rails 3 is a target for this project (see https://github.com/jpmcgrath/shortener#dependencies and https://github.com/jpmcgrath/shortener#installation)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ok, makes sense 👍

@pinnymz
Copy link
Author

pinnymz commented Oct 12, 2021

Revisiting, any thoughts on merging this?

@craigmcnamara
Copy link
Collaborator

craigmcnamara commented Dec 1, 2021

@pinnymz I'd reiterate that caching is best left as an application level concern. You can wrap your calls to shortener however you see fit instead of complicating the internals of Shortener.

Ex:

Rails.cache.fetch("cache-key", expires_in: 30.minutes) do
  Shortener::ShortenedUrl.generate("http://example.com")
end

This has the advantage of allowing you to control the cache behavior based on the application instead of applying a blanket cache policy to the shortener. In my apps using Shortener there are a variety of situations and a single cache config would be inappropriate.

@pinnymz
Copy link
Author

pinnymz commented Dec 16, 2021

@craigmcnamara in our earlier exchange, I pointed out that this was listed as a future improvement in the README, and you mentioned leaving this up to @jpmcgrath. Has something changed in that regard since that time?

Your idea about using Rails.cache explicitly will certainly work for those that want tight control over how the caching will work. For those looking for a batteries-included solution, this feature could still be useful and unobtrusive by comparison. Additionally, your approach won't be very elegant if the shortened urls are generated solely by helper methods within a view.

Lastly, the logic gets more cumbersome if additional params are being passed to the url. I've handled this by appending an MD5 hash in that scenario, but I'm not convinced this is the best solution. And if someone does come up with something better, certainly it would be better for a general solution to a common problem to be handled by a gem upgrade.

I'm currently using this feature in a production system, and not losing any sleep over the lack of fine-grained control. Given that this would be disabled by default, why not let people choose what works best for their system?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants