Skip to content

Commit

Permalink
add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Sianao committed Nov 26, 2024
1 parent 551d192 commit a801e13
Show file tree
Hide file tree
Showing 9 changed files with 538 additions and 436 deletions.
75 changes: 75 additions & 0 deletions cache/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cache

import (
"context"
"github.com/redis/go-redis/v9"
"io"
"net/http"
"os"
"path/filepath"
)

const (
Young = "Young_Obj"
Old = "Old_Obj"
Station = 2
)

type Redis struct {
db *redis.Client
c chan string
}

func Init() *Redis {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
if rdb.Ping(context.Background()).Err() != nil {
return &Redis{}
}
c := make(chan string)
go func() {
for v := range c {
resp, _ := http.Get(v)
os.MkdirAll(filepath.Dir("cache/"+v), os.ModePerm)
fd, _ := os.Create("cache/" + v)
io.Copy(fd, resp.Body)
fd.Close()
resp.Body.Close()
rdb.HIncrBy(context.Background(), Old, v, 1)
}
}()
return &Redis{
db: rdb,
c: c,
}
}
func (c *Redis) Nil() bool {
return c.db == nil
}
func (c *Redis) Exists(key string) bool {
if c.Nil() {
return false
}
res, _ := c.db.HExists(context.Background(), Old, key).Result()
return res
}
func (c *Redis) Incr(fd string) {
if c.Nil() {
return
}
//有序列表 增加
val, _ := c.db.HIncrBy(context.Background(), Young, fd, 1).Result()
// upgrade 策略
if val > Station {
c.Upgrade(fd)
}
}
func (c *Redis) Upgrade(key string) {
if c.Nil() {
return
}
c.c <- key
}
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ module github.com/sianao/gitproxy

go 1.22.5

require github.com/gorilla/mux v1.8.1
require (
github.com/dustin/go-humanize v1.0.1
github.com/gorilla/mux v1.8.1
github.com/redis/go-redis/v9 v9.7.0
)

require github.com/dustin/go-humanize v1.0.1 // indirect
require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
2 changes: 1 addition & 1 deletion handler/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func urlProcess(w http.ResponseWriter, r *http.Request) string {
}
}
func NewHandler(route *mux.Router) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/" || strings.HasPrefix(r.RequestURI, "/_next/") {
router.ServeHTTP(w, r, route)
return
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/sianao/gitproxy/cache"
"log"
"net/http"
"time"
Expand All @@ -10,7 +11,8 @@ import (
)

func main() {
newRouter := router.NewRouter()
c := cache.Init()
newRouter := router.NewRouter(c)
srv := &http.Server{
Handler: handler.NewHandler(newRouter),
Addr: "0.0.0.0:8888",
Expand Down
21 changes: 16 additions & 5 deletions router/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router

import (
"fmt"
"github.com/sianao/gitproxy/cache"
"net/http"
"strings"

Expand All @@ -21,7 +22,7 @@ func ServeHTTP(w http.ResponseWriter, req *http.Request, route *mux.Router) {
// https://raw.githubusercontent.com/laurent22/joplin/e652db05e1ba47725249a6ff543628aeeb32fad7/.gitignore
// https://raw.githubusercontent.com/laurent22/joplin/android-v3.2.2/.gitignore
// 建立新的router 这里先建立好 方便后续的router
func NewRouter() *mux.Router {
func NewRouter(c *cache.Redis) *mux.Router {
route := mux.NewRouter()
route.HandleFunc("/git-upload-pack", func(w http.ResponseWriter, r *http.Request) {
userBaisc := r.Context().Value(&moudule.B).([]string)
Expand All @@ -48,14 +49,23 @@ func NewRouter() *mux.Router {
vars := mux.Vars(r)
userBaisc := r.Context().Value(&moudule.B).([]string)
var address = fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s", userBaisc[0], userBaisc[1], vars["version"], vars["file"])
c.Incr(address)
if c.Exists(address) {
http.FileServer(http.Dir("./cache")).ServeHTTP(w, r)
return
}
service.PacketProxy(w, r, address)

})
route.HandleFunc("/archive/refs/tags/{tags}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

userBaisc := r.Context().Value(&moudule.B).([]string)
var address = fmt.Sprintf("https://github.com/%s/%s/archive/refs/tags/%s", userBaisc[0], userBaisc[1], vars["tags"])
c.Incr(address)
if c.Exists(address) {
http.FileServer(http.Dir("./cache")).ServeHTTP(w, r)
return
}
service.PacketProxy(w, r, address)

})
Expand All @@ -65,16 +75,17 @@ func NewRouter() *mux.Router {
http.Error(w, "bad address", 512)
return
}

if userBaisc[2] != "raw" && !strings.HasPrefix(r.RequestURI, "/blob") {
http.Error(w, "bad address", 512)
return
}
r.RequestURI = strings.TrimPrefix(r.RequestURI, "/blob")

var address = fmt.Sprintf("https://raw.githubusercontent.com/%s/%s%s",
userBaisc[0], userBaisc[1], r.RequestURI)

c.Incr(address)
if c.Exists(address) {
http.FileServer(http.Dir("./cache")).ServeHTTP(w, r)
}
service.PacketProxy(w, r, address)

})
Expand Down
74 changes: 37 additions & 37 deletions src/theme/AppAppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ export default function AppAppBar() {
<StyledToolbar variant="dense" disableGutters>
<Box sx={{ flexGrow: 1, display: 'flex', alignItems: 'center', px: 0 }}>
{<GitHubIcon sx={{ color: textColor}}></GitHubIcon>}
<Box sx={{ display: { xs: 'none', md: 'flex' } }}>
<Button variant="text" color="info" size="small">
Features
</Button>
<Button variant="text" color="info" size="small">
Private
</Button>
<Button variant="text" color="info" size="small">
FAQ
</Button>
<Button variant="text" color="info" size="small">
About
</Button>
</Box>
{/*<Box sx={{ display: { xs: 'none', md: 'flex' } }}>*/}
{/* <Button variant="text" color="info" size="small">*/}
{/* Features*/}
{/* </Button>*/}
{/* <Button variant="text" color="info" size="small">*/}
{/* Private*/}
{/* </Button>*/}
{/* <Button variant="text" color="info" size="small">*/}
{/* FAQ*/}
{/* </Button>*/}
{/* <Button variant="text" color="info" size="small">*/}
{/* About*/}
{/* </Button>*/}
{/*</Box>*/}
</Box>
<Box
sx={{
Expand All @@ -77,12 +77,12 @@ export default function AppAppBar() {
alignItems: 'center',
}}
>
<Button color="primary" variant="text" size="small">
Sign in
</Button>
<Button color="primary" variant="contained" size="small">
Sign up
</Button>
{/*<Button color="primary" variant="text" size="small">*/}
{/* Sign in*/}
{/*</Button>*/}
{/*<Button color="primary" variant="contained" size="small">*/}
{/* Sign up*/}
{/*</Button>*/}
<ColorModeIconDropdown />
</Box>
{/* <StyledSimpleCodeEditor></StyledSimpleCodeEditor> */}
Expand Down Expand Up @@ -112,23 +112,23 @@ export default function AppAppBar() {
<CloseRoundedIcon />
</IconButton>
</Box>
<MenuItem>Features</MenuItem>
<MenuItem>Testimonials</MenuItem>
<MenuItem>Highlights</MenuItem>
<MenuItem>Pricing</MenuItem>
<MenuItem>FAQ</MenuItem>
<MenuItem>Blog</MenuItem>
<Divider sx={{ my: 3 }} />
<MenuItem>
<Button color="primary" variant="contained" fullWidth>
Sign up
</Button>
</MenuItem>
<MenuItem>
<Button color="primary" variant="outlined" fullWidth>
Sign in
</Button>
</MenuItem>
{/*<MenuItem>Features</MenuItem>*/}
{/*<MenuItem>Testimonials</MenuItem>*/}
{/*<MenuItem>Highlights</MenuItem>*/}
{/*<MenuItem>Pricing</MenuItem>*/}
{/*<MenuItem>FAQ</MenuItem>*/}
{/*<MenuItem>Blog</MenuItem>*/}
{/*<Divider sx={{ my: 3 }} />*/}
{/*<MenuItem>*/}
{/* <Button color="primary" variant="contained" fullWidth>*/}
{/* Sign up*/}
{/* </Button>*/}
{/*</MenuItem>*/}
{/*<MenuItem>*/}
{/* <Button color="primary" variant="outlined" fullWidth>*/}
{/* Sign in*/}
{/* </Button>*/}
{/*</MenuItem>*/}
</Box>
</Drawer>
</Box>
Expand Down
Loading

0 comments on commit a801e13

Please sign in to comment.