go-github icon indicating copy to clipboard operation
go-github copied to clipboard

httpcache doesn't really cache much right now

Open yuriy-yarosh opened this issue 4 years ago • 3 comments

package main

import (
	"context"
	"fmt"

	"github.com/google/go-github/v45/github"
	"github.com/gregjones/httpcache"
	"github.com/gregjones/httpcache/leveldbcache"
	"github.com/syndtr/goleveldb/leveldb"
	"github.com/syndtr/goleveldb/leveldb/opt"
	"golang.org/x/oauth2"
	"net/http"
	"os"
)

var token = os.Getenv("GITHUB_API_TOKEN")

func main() {
	cache, err := leveldb.OpenFile(".cache", nil)
	if err != nil {
		panic(err)
	}

	client := github.NewClient(&http.Client{
		Transport: &oauth2.Transport{
			Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}),
			Base:   httpcache.NewTransport(leveldbcache.NewWithDB(cache)).Transport,
		},
	})

	all, _, err := client.Organizations.ListAll(context.Background(), &github.OrganizationsListOptions{
		ListOptions: github.ListOptions{
			Page:    0,
			PerPage: 30,
		},
	})
	if err != nil {
		panic(err)
	}

	for _, org := range all {
		fmt.Fprintln(os.Stderr, org.GetID())
	}
}

Gives me

ls -alh .cache
загалом 24K
drwxr-xr-x 2 yura users 4,0K лип  4 04:55 .
drwxr-xr-x 6 yura users 4,0K лип  4 04:54 ..
-rw-r--r-- 1 yura users    0 лип  4 04:55 000014.log
-rw-r--r-- 1 yura users   16 лип  4 04:55 CURRENT
-rw-r--r-- 1 yura users   16 лип  4 04:55 CURRENT.bak
-rw-r--r-- 1 yura users    0 лип  4 04:47 LOCK
-rw-r--r-- 1 yura users 3,8K лип  4 04:55 LOG
-rw-r--r-- 1 yura users   41 лип  4 04:55 MANIFEST-000015

I don't really understand why not much is cached via httpcache, am I propagating Client Transports in wrong order ?... This is something worth documenting.

yuriy-yarosh avatar Jul 04 '22 02:07 yuriy-yarosh

httpcache has come up a number of times in the past... please check out these threads, as they might give you some insight as to what is going on: https://github.com/google/go-github/issues?q=is%3Aissue+httpcache

gmlewis avatar Jul 04 '22 02:07 gmlewis

@gmlewis it seams they didn't accept that EOF PR in the end for some reason, and httpcache had been dropped even out of k8s.

yuriy-yarosh avatar Jul 04 '22 11:07 yuriy-yarosh

Closing due to inactivity.

gmlewis avatar Mar 17 '25 22:03 gmlewis