-
Notifications
You must be signed in to change notification settings - Fork 436
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
Make caching easier for non-memory caches. #345
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,27 +226,63 @@ Typhoeus includes built in support for caching. In the following example, if the | |
|
||
```ruby | ||
class Cache | ||
attr_accessor :memory | ||
|
||
def initialize | ||
@memory = {} | ||
end | ||
|
||
def get(request) | ||
memory[request] | ||
@memory[request] | ||
end | ||
|
||
def set(request, response) | ||
memory[request] = response | ||
@memory[request] = response | ||
end | ||
end | ||
|
||
Typhoeus::Config.cache = Cache.new | ||
|
||
Typhoeus.get("www.example.com") == Typhoeus.get("www.example.com") | ||
Typhoeus.get("www.example.com").cached? | ||
#=> false | ||
Typhoeus.get("www.example.com").cached? | ||
#=> true | ||
``` | ||
|
||
For use with [Dalli](https://github.com/mperham/dalli): | ||
|
||
```ruby | ||
class Cache | ||
def initialize | ||
@client = Dalli::Client.new | ||
end | ||
|
||
def get(request) | ||
@client.get(request.cache_key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While that's true, Dalli handles this internally by truncating and using Also take a look at this in terms of using CRC32: It's fine for a ruby Hash since hash equivalence is not enough for a match, On Fri, Dec 27, 2013 at 3:54 PM, Hans Hasselberg
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. Sounds good. |
||
end | ||
|
||
def set(request, response) | ||
@client.set(request.cache_key, response) | ||
end | ||
end | ||
|
||
Typhoeus::Config.cache = Cache.new | ||
``` | ||
|
||
For use with Rails: | ||
|
||
```ruby | ||
class Cache | ||
def get(request) | ||
Rails.cache.read(request) | ||
end | ||
|
||
def set(request, response) | ||
Rails.cache.write(request, response) | ||
end | ||
end | ||
|
||
Typhoeus::Config.cache = Cache.new | ||
``` | ||
|
||
### Direct Stubbing | ||
|
||
Hydra allows you to stub out specific urls and patterns to avoid hitting | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. In addition to pointing out
#cached?
, I changed it because it's not actually true for non-memory cachers that serialize and then deserialize the response object.