If GithubAPI::enable_cache
is set to true then Github JSON responses
will be cached locally using JsonStore
.
Cache can be used to limit the number of access on the API and lighten the rate limit on your github key.
Usage:
var api = new GithubAPI(get_github_oauth)
api.enable_cache = true
var name = "nitlang/nit"
assert not api.has_cache(name)
var repo = api.get_repo(name) # load from GitHub
#assert api.has_cache(name) FIXME bring back this assert
repo = api.get_repo(name) # load from cache
api.clear_cache
assert not api.has_cache(name)
Serializable::inspect
to show more useful information
curl :: native_curl
Binding of C libCurl which allow us to interact with network.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatsdeserialize_json
and JsonDeserializer
serialize_to_json
and JsonSerializer
core :: union_find
union–find algorithm using an efficient disjoint-set data structure
# Enable caching on Github API accesses.
#
# If `GithubAPI::enable_cache` is set to true then Github JSON responses
# will be cached locally using `JsonStore`.
#
# Cache can be used to limit the number of access on the API and lighten
# the rate limit on your github key.
#
# Usage:
#
# ~~~
# var api = new GithubAPI(get_github_oauth)
# api.enable_cache = true
#
# var name = "nitlang/nit"
# assert not api.has_cache(name)
# var repo = api.get_repo(name) # load from GitHub
# #assert api.has_cache(name) FIXME bring back this assert
# repo = api.get_repo(name) # load from cache
#
# api.clear_cache
# assert not api.has_cache(name)
# ~~~
module cache
intrude import github::api
import json::store
redef class GithubAPI
# Enable caching for this client.
# Default is `false`.
var enable_cache = false is writable
# JsonStore used to cache data.
#
# Default directory is `".github_data/"`.
var store = new JsonStore(".github_data/") is writable, lazy
# Delete the cache directory.
fun clear_cache do store.clear
# If no cache data is found for `key` then json is loaded from Github API.
redef fun get(key, headers, data) do
if not enable_cache then return super
if store.has_key(key) then
# print "Get {key} (cache)" # debug
was_error = false
return deserialize(store.load_object(key).to_json)
end
var obj = super
if not was_error and obj isa Serializable then
cache(key, obj)
end
return obj
end
# Save `json` data in cache under `key`.
private fun cache(key: String, obj: Serializable) do
# print "Cache key {key}" # debug
store.store_object(key, obj.to_json.parse_json.as(JsonObject))
end
# Check if a cache file exists for `key`.
fun has_cache(key: String): Bool do
return store.has_key(key)
end
end
lib/github/cache.nit:15,1--82,3