Skip to content

Commit 9473b9a

Browse files
committed
<添加案例><freecache><基础>
1 parent db8cbe1 commit 9473b9a

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

demo/freecache/http/main.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/coocood/freecache"
6+
"runtime/debug"
7+
"fmt"
8+
)
9+
10+
func main() {
11+
cacheSize := 100 * 1024 * 1024
12+
cache := freecache.NewCache(cacheSize)
13+
debug.SetGCPercent(20)
14+
key := []byte("abc")
15+
val := []byte("def")
16+
expire := 60 // expire in 60 seconds
17+
cache.Set(key, val, expire)
18+
gin.SetMode(gin.ReleaseMode)
19+
r := gin.Default()
20+
r.GET("/", func(c *gin.Context) {
21+
got, err := cache.Get(key)
22+
if err != nil {
23+
fmt.Println(err)
24+
} else {
25+
fmt.Println(string(got))
26+
}
27+
28+
fmt.Println("entry count ", cache.EntryCount())
29+
c.JSON(200, gin.H{
30+
"message": "pong",
31+
})
32+
})
33+
r.Run() // listen and serve on 0.0.0.0:8080
34+
}

demo/freecache/main.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"github.com/coocood/freecache"
5+
"runtime/debug"
6+
"fmt"
7+
)
8+
9+
func main() {
10+
cacheSize := 100 * 1024 * 1024
11+
cache := freecache.NewCache(cacheSize)
12+
debug.SetGCPercent(20)
13+
key := []byte("abc")
14+
val := []byte("def")
15+
expire := 60 // expire in 60 seconds
16+
cache.Set(key, val, expire)
17+
got, err := cache.Get(key)
18+
if err != nil {
19+
fmt.Println(err)
20+
} else {
21+
fmt.Println(string(got))
22+
}
23+
affected := cache.Del(key)
24+
fmt.Println("deleted key ", affected)
25+
fmt.Println("entry count ", cache.EntryCount())
26+
27+
}

demo/stackimpact/main.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/stackimpact/stackimpact-go"
8+
)
9+
10+
func handler(w http.ResponseWriter, r *http.Request) {
11+
fmt.Fprintf(w, "Hello world!")
12+
}
13+
14+
func main() {
15+
agent := stackimpact.Start(stackimpact.Options{
16+
AgentKey: "fa34557e1650b4847f0242d6ece649a99e46fb3e",
17+
AppName: "iceinto-demo",
18+
Debug: true,
19+
})
20+
21+
http.HandleFunc(agent.ProfileHandlerFunc("/", handler))
22+
http.ListenAndServe(":8080", nil)
23+
}

0 commit comments

Comments
 (0)