From: Alexandre Terrasa Date: Thu, 22 Jan 2015 10:49:05 +0000 (+0100) Subject: lib/github: implements caching to maximize rate limit. X-Git-Tag: v0.7.1~17^2 X-Git-Url: http://nitlanguage.org lib/github: implements caching to maximize rate limit. Signed-off-by: Alexandre Terrasa --- diff --git a/.gitignore b/.gitignore index 5cd49a0..2156619 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,4 @@ nitunit.xml *.stub.nit.[ch] .metadata/* +.github_data diff --git a/lib/github/api.nit b/lib/github/api.nit index b979b5d..2655ae3 100644 --- a/lib/github/api.nit +++ b/lib/github/api.nit @@ -133,7 +133,7 @@ class GithubAPI # Load the json object from Github. # See `GithubEntity::load_from_github`. - private fun load_from_github(key: String): JsonObject do + protected fun load_from_github(key: String): JsonObject do message(1, "Get {key} (github)") var res = get(key) if was_error then return new JsonObject diff --git a/lib/github/cache.nit b/lib/github/cache.nit new file mode 100644 index 0000000..559b4a8 --- /dev/null +++ b/lib/github/cache.nit @@ -0,0 +1,80 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# 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 = "privat/nit" +# assert not api.has_cache(name) +# var repo = api.load_repo(name) # load from GitHub +# assert api.has_cache(name) +# repo = api.load_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 load_from_github(key) do + if not enable_cache then return super + if store.has_key(key) then + message(1, "Get {key} (cache)") + was_error = false + return store.load_object(key) + end + var obj = super + if not was_error then cache(key, obj) + return obj + end + + # Save `json` data in cache under `key`. + private fun cache(key: String, json: JsonObject) do + message(2, "Cache key {key}") + store.store_object(key, json) + end + + # Check if a cache file exists for `key`. + fun has_cache(key: String): Bool do + return store.has_key(key) + end +end diff --git a/lib/github/github.nit b/lib/github/github.nit index 778f826..9bc63a8 100644 --- a/lib/github/github.nit +++ b/lib/github/github.nit @@ -15,4 +15,4 @@ # Github API related features. module github -import api +import cache