Skip to content

Caching is not working properly with Repositories.GetContents #1503

@m4ns0ur

Description

@m4ns0ur

Not sure if it's go-github issue or httpcache one.

I'm trying to cache Repositories.GetContents responses to avoid my rate limit's burning.

package main

import (
	"context"
	"fmt"

	"github.com/google/go-github/github"
	"github.com/gregjones/httpcache"
	"github.com/gregjones/httpcache/diskcache"
)

func main() {
	cl := github.NewClient(httpcache.NewTransport(diskcache.New(".")).Client())
	_, _, r, _ := cl.Repositories.GetContents(context.Background(), "google", "go-github", "LICENSE", nil)
	fmt.Println(r)
}

At first run (diskcache file is going to be created for the first time), it works

$ date && go run main.go 
Thu 23 Apr 2020 02:01:04 PM CEST
&{0xc00014c090 0 0 0 0  github.Rate{Limit:60, Remaining:5, Reset:github.Timestamp{2020-04-23 14:20:15 +0200 CEST}}}
...
$ date && go run main.go 
Thu 23 Apr 2020 02:01:27 PM CEST
&{0xc000114000 0 0 0 0  github.Rate{Limit:60, Remaining:5, Reset:github.Timestamp{2020-04-23 14:20:15 +0200 CEST}}}

But if you run it again, after 60s (Github API's max-age), it doesn't work anymore

$ date && go run main.go 
Thu 23 Apr 2020 02:02:17 PM CEST
&{0xc000112000 0 0 0 0  github.Rate{Limit:60, Remaining:4, Reset:github.Timestamp{2020-04-23 14:20:14 +0200 CEST}}}
...
$ date && go run main.go 
Thu 23 Apr 2020 02:02:19 PM CEST
&{0xc00018a000 0 0 0 0  github.Rate{Limit:60, Remaining:3, Reset:github.Timestamp{2020-04-23 14:20:14 +0200 CEST}}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions