Merge: Safe call operator
[nit.git] / lib / github / cache.nit
index e4d41c3..dbb4ad0 100644 (file)
 # var api = new GithubAPI(get_github_oauth)
 # api.enable_cache = true
 #
-# var name = "privat/nit"
+# var name = "nitlang/nit"
 # assert not api.has_cache(name)
-# var repo = api.load_repo(name) # load from GitHub
+# var repo = api.get_repo(name) # load from GitHub
 # #assert api.has_cache(name) FIXME bring back this assert
-# repo = api.load_repo(name) # load from cache
+# repo = api.get_repo(name) # load from cache
 #
 # api.clear_cache
 # assert not api.has_cache(name)
@@ -55,22 +55,24 @@ redef class GithubAPI
        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
+       redef fun get(key, headers, data) do
                if not enable_cache then return super
                if store.has_key(key) then
-                       message(1, "Get {key} (cache)")
+                       # print "Get {key} (cache)" # debug
                        was_error = false
-                       return store.load_object(key)
+                       return deserialize(store.load_object(key).to_json)
                end
                var obj = super
-               if not was_error then cache(key, obj)
+               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, json: JsonObject) do
-               message(2, "Cache key {key}")
-               store.store_object(key, json)
+       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`.