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

feature: local cache #24

Closed
icyleaf opened this issue Aug 20, 2018 · 3 comments
Closed

feature: local cache #24

icyleaf opened this issue Aug 20, 2018 · 3 comments

Comments

@icyleaf
Copy link
Owner

icyleaf commented Aug 20, 2018

Better to debug and improve the speed for developing third party API.

Logic

  • cache key: hash url with params as cache key with MD5 or use given filename with path.
  • cache body: stores full response(status code, headers and body) to local file, at least stores the body of response.
  • cache directory: You can set it
  • expires time: stored forever if not set
  • update strategy: check cache related headers or hash body and compares, i don't know.

Cache key

generate algorithm md5(verb + url + body)

Cache response

Store response into two files:

  • metadata: Stored status_code and headers in JSON format.
  • body: Original body of response, so it can be binary data.

metadata JSON structure:

{
  "status_code": 200,
  "headers": {
    "content-type": "application/json",
  },
}

Expires time

Supports Int32, Time::Span is enough.

Examples

Use it with single call

Halite.cache.get "https://httpbin.ogr/get"
Halite.cache(directory: "cache", expired: 1.hour).get "https://httpbin.ogr/get"

# Loads the content of "cache/get.txt" 
Halite.cache(file: "get.txt").get "https://httpbin.ogr/get"

Use it with instance client:

client = Halite::Client.new do |options|
  options.cache = true
end

client = Halite::Client.new do |options|
  options.cache(directory: "cache", expired: 1.hour)
end

client.get "https://httpbin.ogr/get"

This request will store into cache path: "cache/{cache_key}" with two files:

metadata.json:

{
  "status_code": 200,
  "headers": {
    "content-type": "application/json",
  },
}

{cache_key}.cache

{"args":{"name":"foobar"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"Halite/0.3.2"},"origin":"60.206.194.34","url":"http://httpbin.org/get?name=foobar"}

Load from file

It supports load cache from file, just passes file argument, Cache will load the content of file and return to Halite::Response no matter request whatever is.

PR

#35

@icyleaf
Copy link
Owner Author

icyleaf commented Aug 28, 2018

#29

@icyleaf
Copy link
Owner Author

icyleaf commented Aug 30, 2018

My gosh, this feature make me commited a lot to prepare.

@icyleaf
Copy link
Owner Author

icyleaf commented Sep 3, 2018

Finally, It had been done.

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

No branches or pull requests

1 participant